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
use core::cmp::min;

use crate::{
    constants::{self},
    ExternalEnvTypes, HostExt, JournalInspectTr, MegaContext, MegaSpecId,
};
use alloy_evm::Database;
use alloy_primitives::{keccak256, Bytes, U256};
use revm::{
    context::ContextTr,
    handler::instructions::{EthInstructions, InstructionProvider},
    interpreter::{
        as_usize_or_fail, gas, gas_or_fail,
        instructions::{self, control, utility::IntoAddress},
        interpreter::EthInterpreter,
        interpreter_types::{InputsTr, LoopControl, MemoryTr, RuntimeFlag},
        resize_memory, CallScheme, FrameInput, Instruction, InstructionContext, InstructionResult,
        InstructionTable, InterpreterAction, InterpreterTypes, SStoreResult, Stack,
    },
    primitives::KECCAK_EMPTY,
};

/// `MegaInstructions` is the instruction table for `MegaETH`.
///
/// This instruction table implements a multi-dimensional gas model and customizes certain opcodes
/// for `MegaETH` specifications:
///
/// # Multi-Dimensional Gas Model
///
/// All instructions track gas usage across multiple dimensions:
/// - **Compute Gas**: Standard EVM operation costs (arithmetic, control flow, memory, etc.)
/// - **Storage Gas**: Dynamic costs for persistent storage operations (SSTORE, CREATE, CALL with
///   transfer)
/// - **Log Storage Gas**: Additional costs for persisting event logs (10x standard costs)
///
/// This separation allows for independent pricing and limiting of different resource types.
///
/// # Customized Opcodes
///
/// ## LOG Opcodes (LOG0-LOG4)
/// - Compute gas: Standard EVM costs (375 + 375×topics + `8×data_bytes`)
/// - Storage gas: 10x multiplier (3,750×topics + `80×data_bytes`)
/// - Data limit enforcement: Halts when total transaction data exceeds 3.125 MB
///
/// ## SELFDESTRUCT Opcode
/// - Disabled in Mini-Rex, Rex, and Rex1 specs
/// - Re-enabled in Rex2 with EIP-6780 semantics
/// - When disabled, halts with `InvalidFEOpcode` to prevent contract destruction
///
/// ## SSTORE Opcode
/// - Compute gas: Standard EIP-2200/EIP-2929 costs
/// - Storage gas: Dynamic bucket-based costs only when setting zero → non-zero
/// - Data/KV limit enforcement: Tracks 40 bytes + 1 KV update per storage slot modification
///
/// ## CREATE/CREATE2 Opcodes
/// - Compute gas: Standard costs (32,000 for CREATE, 6 gas/word for CREATE2 hashing)
/// - Storage gas: Dynamic bucket-based costs for new account creation
/// - Gas forwarding: 98/100 rule (2% withheld vs. standard 1.5%)
/// - Data/KV tracking: 40 bytes + 1 KV update per account creation
///
/// ## CALL-like Opcode
/// - Compute gas: Standard call costs
/// - Storage gas: Dynamic bucket-based costs for new account creation (when transferring to empty
///   account)
/// - REX4+: Value-transferring `CALL`/`CALLCODE` receives additional `STORAGE_CALL_STIPEND` for
///   storage-gas-heavy operations such as `LOG`
/// - REX4+: Compute gas remains capped at the original `forwarded_gas + CALL_STIPEND`, so the extra
///   stipend cannot be used for pure computation
/// - Gas forwarding: 98/100 rule (2% withheld vs. standard 1.5%)
/// - Oracle detection: Handled at frame level (in `frame_init`), applies gas detention for both
///   direct transaction calls and internal CALL operations
/// - Data/KV tracking: 40 bytes + 2 KV updates when transferring to empty account
///
/// ## Volatile Data Access Opcodes
/// Block environment opcodes (TIMESTAMP, NUMBER, COINBASE, DIFFICULTY, GASLIMIT, BASEFEE,
/// BLOCKHASH, BLOBBASEFEE, BLOBHASH) and beneficiary-accessing opcodes (BALANCE, EXTCODESIZE,
/// EXTCODECOPY, EXTCODEHASH) implement immediate gas detention to prevent `DoS` attacks.
///
/// # Gas Detention Mechanism
///
/// When volatile data (block environment, beneficiary, or oracle) is accessed, the system
/// implements a gas detention mechanism:
/// 1. The compute gas limit is lowered based on the type of volatile data:
///    - Block environment or beneficiary: `BLOCK_ENV_ACCESS_COMPUTE_GAS` (20M gas)
///    - Oracle contract: `ORACLE_ACCESS_COMPUTE_GAS` (1M gas pre-Rex3, 20M gas Rex3+)
///
///    In pre-REX4, this is an **absolute** cap on total compute gas.
///    In REX4+, this is a **relative** cap: `usage_at_access + cap`.
/// 2. Most restrictive limit wins: If multiple volatile data types are accessed, the minimum (most
///    restrictive) effective limit applies, regardless of access order
/// 3. Detained gas is tracked and refunded at transaction end
/// 4. Users only pay for actual work performed, not for enforcement gas
/// 5. This prevents `DoS` attacks while maintaining fair gas accounting
///
/// # Instruction Layering Architecture
///
/// ## Extension Modules (Inner → Outer)
///
/// Each opcode handler is composed of one or more extension module wrappers, applied from
/// innermost (closest to revm) to outermost:
///
/// 1. **`compute_gas_ext`** — Tracks compute gas usage for every opcode. Wraps the raw revm
///    instruction and records how much gas was consumed.
/// 2. **`storage_gas_ext`** — Adds dynamic storage gas costs (SSTORE, CALL with value transfer,
///    CREATE, LOG). Wraps `compute_gas_ext` handlers.
/// 3. **`additional_limit_ext`** — Enforces multidimensional resource limits (data size, KV
///    updates). Wraps `storage_gas_ext` handlers.
/// 4. **`forward_gas_ext`** — Enforces the 98/100 gas forwarding rule for CALL-like and CREATE
///    opcodes. Wraps `storage_gas_ext` handlers.
/// 5. **`volatile_data_ext`** — Applies gas detention on volatile data access (block env,
///    beneficiary, oracle) and pre-execution disable checks (Rex4+). Wraps `compute_gas_ext` or
///    `forward_gas_ext` handlers depending on the opcode.
///
/// ## Spec Progression and Opcode Overrides
///
/// Each spec builds on the previous one. Only the opcodes that change are listed:
///
/// - **EQUIVALENCE**: Standard revm mainnet instruction table (no custom wrappers).
/// - **`MINI_REX`** (base custom table): All 256 opcodes initialized from scratch.
///   - Most opcodes: `compute_gas_ext::*`
///   - Block env opcodes (TIMESTAMP, NUMBER, etc.): `volatile_data_ext::*`
///   - BALANCE, EXTCODESIZE, EXTCODECOPY, EXTCODEHASH: `volatile_data_ext::*`
///   - SLOAD: `compute_gas_ext::sload`
///   - SSTORE: `additional_limit_ext` → `storage_gas_ext` → `compute_gas_ext`
///   - LOG0–LOG4: `additional_limit_ext` → `storage_gas_ext` → `compute_gas_ext`
///   - CALL: `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext`
///   - CREATE, CREATE2: `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext`
///   - CALLCODE, DELEGATECALL, STATICCALL: `compute_gas_ext::*` (bug: missing `forward_gas_ext`)
///   - SELFDESTRUCT: disabled (`control::invalid`)
/// - **REX / REX1** (extends `MINI_REX)`:
///   - CALLCODE: `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext` (bugfix)
///   - DELEGATECALL: `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext` (bugfix)
///   - STATICCALL: `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext` (bugfix)
/// - **REX2** (extends REX):
///   - SELFDESTRUCT: `compute_gas_ext::selfdestruct` (re-enabled with EIP-6780)
/// - **REX3** (extends REX2):
///   - SLOAD: `volatile_data_ext::sload` → `compute_gas_ext::sload` (oracle gas detention)
/// - **REX4** (extends REX3):
///   - CALL: `volatile_data_ext` → `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext`
///   - STATICCALL: `volatile_data_ext` → `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext`
///   - DELEGATECALL: `volatile_data_ext` → `forward_gas_ext` → `storage_gas_ext` →
///     `compute_gas_ext`
///   - CALLCODE: `volatile_data_ext` → `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext`
///
/// # Assumptions
///
/// This instruction table is only used when the `MINI_REX` spec (or later) is enabled, so we can
/// safely assume that all features before and including Mini-Rex are enabled.
#[derive(Clone)]
pub struct MegaInstructions<DB: Database, ExtEnvs: ExternalEnvTypes> {
    spec: MegaSpecId,
    inner: EthInstructions<EthInterpreter, MegaContext<DB, ExtEnvs>>,
}

impl<DB: Database, ExtEnvs: ExternalEnvTypes> core::fmt::Debug for MegaInstructions<DB, ExtEnvs> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("MegaethInstructions").field("spec", &self.spec).finish_non_exhaustive()
    }
}

impl<DB: Database, ExtEnvs: ExternalEnvTypes> MegaInstructions<DB, ExtEnvs> {
    /// Create a new `MegaethInstructions` with the given spec id.
    pub fn new(spec: MegaSpecId) -> Self {
        let instruction_table = match spec {
            MegaSpecId::EQUIVALENCE => EthInstructions::new_mainnet(),
            MegaSpecId::MINI_REX => EthInstructions::new(mini_rex::instruction_table::<
                EthInterpreter,
                MegaContext<DB, ExtEnvs>,
            >()),
            MegaSpecId::REX | MegaSpecId::REX1 => EthInstructions::new(rex::instruction_table::<
                EthInterpreter,
                MegaContext<DB, ExtEnvs>,
            >()),
            MegaSpecId::REX2 => EthInstructions::new(rex2::instruction_table::<
                EthInterpreter,
                MegaContext<DB, ExtEnvs>,
            >()),
            MegaSpecId::REX3 => EthInstructions::new(rex3::instruction_table::<
                EthInterpreter,
                MegaContext<DB, ExtEnvs>,
            >()),
            MegaSpecId::REX4 => EthInstructions::new(rex4::instruction_table::<
                EthInterpreter,
                MegaContext<DB, ExtEnvs>,
            >()),
            MegaSpecId::REX5 => EthInstructions::new(rex5::instruction_table::<
                EthInterpreter,
                MegaContext<DB, ExtEnvs>,
            >()),
        };
        Self { spec, inner: instruction_table }
    }
}

impl<DB: Database, ExtEnvs: ExternalEnvTypes> InstructionProvider
    for MegaInstructions<DB, ExtEnvs>
{
    type Context = MegaContext<DB, ExtEnvs>;
    type InterpreterTypes = EthInterpreter;

    fn instruction_table(&self) -> &InstructionTable<Self::InterpreterTypes, Self::Context> {
        self.inner.instruction_table()
    }
}

mod rex {
    use super::*;

    /// Returns the instruction table for the `REX` and `REX1` specs.
    ///
    /// Changes from Mini-Rex (bugfix — adds missing `forward_gas_ext` wrapping):
    /// - CALLCODE: `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext`
    /// - DELEGATECALL: `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext`
    /// - STATICCALL: `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext`
    pub(super) const fn instruction_table<
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ContextTr + JournalInspectTr + ?Sized,
    >() -> [Instruction<WIRE, H>; 256]
    where
        WIRE::Stack: StackInspectTr,
    {
        use revm::bytecode::opcode::*;
        let mut table = mini_rex::instruction_table::<WIRE, H>();

        // Mini-Rex mistakenly not modifying these three call-like opcodes. They are fixed in Rex
        table[CALLCODE as usize] = forward_gas_ext::call_code;
        table[DELEGATECALL as usize] = forward_gas_ext::delegate_call;
        table[STATICCALL as usize] = forward_gas_ext::static_call;

        table
    }
}

mod rex2 {
    use super::*;

    /// Returns the instruction table for the `REX2` spec.
    ///
    /// Changes from Rex:
    /// - SELFDESTRUCT: `compute_gas_ext::selfdestruct` (re-enabled with EIP-6780 semantics)
    pub(super) const fn instruction_table<
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ContextTr + JournalInspectTr + ?Sized,
    >() -> [Instruction<WIRE, H>; 256]
    where
        WIRE::Stack: StackInspectTr,
    {
        use revm::bytecode::opcode::*;
        let mut table = rex::instruction_table::<WIRE, H>();

        table[SELFDESTRUCT as usize] = compute_gas_ext::selfdestruct;

        table
    }
}

mod rex3 {
    use super::*;

    /// Returns the instruction table for the `REX3` spec.
    ///
    /// Changes from Rex2:
    /// - SLOAD: `volatile_data_ext::sload` → `compute_gas_ext::sload` (oracle gas detention). This
    ///   replaces the CALL-based oracle access detection used in earlier specs.
    pub(super) const fn instruction_table<
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ContextTr + JournalInspectTr + ?Sized,
    >() -> [Instruction<WIRE, H>; 256]
    where
        WIRE::Stack: StackInspectTr,
    {
        use revm::bytecode::opcode::*;
        let mut table = rex2::instruction_table::<WIRE, H>();

        // Rex3: SLOAD triggers gas detention for oracle contract access.
        // The host's sload() method marks oracle access in the volatile data tracker,
        // then the detain_gas_ext wrapper applies the compute gas limit.
        table[SLOAD as usize] = volatile_data_ext::sload;

        table
    }
}

mod rex4 {
    use super::*;

    /// Returns the instruction table for the `REX4` spec.
    ///
    /// Changes from Rex3:
    /// - CALL: `volatile_data_ext::call` → `forward_gas_ext` → `storage_gas_ext` →
    ///   `compute_gas_ext`
    /// - STATICCALL: `volatile_data_ext::static_call` → `forward_gas_ext` → `storage_gas_ext` →
    ///   `compute_gas_ext`
    /// - DELEGATECALL: `volatile_data_ext::delegate_call` → `forward_gas_ext` → `storage_gas_ext` →
    ///   `compute_gas_ext`
    /// - CALLCODE: `volatile_data_ext::call_code` → `forward_gas_ext` → `storage_gas_ext` →
    ///   `compute_gas_ext`
    /// - SELFDESTRUCT: `volatile_data_ext::selfdestruct` → `compute_gas_ext::selfdestruct`
    /// - SELFBALANCE: `volatile_data_ext::selfbalance` → `compute_gas_ext::selfbalance`
    ///
    /// The `volatile_data_ext` wrapper checks if the target address is the beneficiary and
    /// volatile data access is disabled — if so, reverts before executing.
    pub(super) const fn instruction_table<
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ContextTr + JournalInspectTr + ?Sized,
    >() -> [Instruction<WIRE, H>; 256]
    where
        WIRE::Stack: StackInspectTr,
    {
        use revm::bytecode::opcode::*;
        let mut table = rex3::instruction_table::<WIRE, H>();

        // Rex4: CALL-like opcodes check for beneficiary volatile access disabled.
        table[CALL as usize] = volatile_data_ext::call;
        table[STATICCALL as usize] = volatile_data_ext::static_call;
        table[DELEGATECALL as usize] = volatile_data_ext::delegate_call;
        table[CALLCODE as usize] = volatile_data_ext::call_code;

        // Rex4: SELFDESTRUCT checks for beneficiary volatile access.
        table[SELFDESTRUCT as usize] = volatile_data_ext::selfdestruct;

        // Rex4: SELFBALANCE checks for beneficiary volatile access (when the executing
        // contract is the beneficiary, SELFBALANCE triggers gas detention).
        table[SELFBALANCE as usize] = volatile_data_ext::selfbalance;

        table
    }
}

mod rex5 {
    use super::*;

    /// Returns the instruction table for the `REX5` spec.
    ///
    /// Changes from Rex4:
    /// - SELFDESTRUCT: `volatile_data_ext::selfdestruct_rex5` → `storage_gas_ext::selfdestruct` →
    ///   `compute_gas_ext::selfdestruct`. The new outer wrapper keeps the beneficiary-volatile
    ///   guard outermost (matching the SSTORE / LOG layering) and slots the new-account storage-gas
    ///   charge between the guard and the compute layer, so disabled-volatile frames short-circuit
    ///   ahead of any storage-layer side effects (account inspection, dynamic gas charge,
    ///   `on_selfdestruct_new_account` record).
    pub(super) const fn instruction_table<
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ContextTr + JournalInspectTr + ?Sized,
    >() -> [Instruction<WIRE, H>; 256]
    where
        WIRE::Stack: StackInspectTr,
    {
        use revm::bytecode::opcode::*;
        let mut table = rex4::instruction_table::<WIRE, H>();

        // REX5: SELFDESTRUCT charges storage gas for new beneficiary accounts,
        // gated behind the beneficiary-volatile guard.
        table[SELFDESTRUCT as usize] = volatile_data_ext::selfdestruct_rex5;

        table
    }
}

/// Macro to record compute gas and check if the limit has been exceeded. If the limit is exceeded,
/// the interpreter halts and returns.
macro_rules! compute_gas {
    ($interpreter:expr, $additional_limit:expr, $gas_used:expr $(,$ret:expr)?) => {
        if !$additional_limit.record_compute_gas($gas_used) {
            $interpreter.halt($additional_limit.exceeding_instruction_result());
            return $($ret)?;
        }
    };
}

/// Macro to run the inner instruction and abort if the instruction result is an error.
macro_rules! run_inner_instruction_or_abort {
    ($inner_fn:path, $context:expr) => {
        let ctx = InstructionContext::<'_, H, WIRE> {
            interpreter: &mut *$context.interpreter,
            host: &mut *$context.host,
        };
        $inner_fn(ctx);
        if $context
            .interpreter
            .bytecode
            .instruction_result()
            .is_some_and(|result| result.is_error())
        {
            return;
        }
    };
}

mod mini_rex {
    use super::*;

    /// Returns the instruction table for the `MINI_REX` spec.
    ///
    /// This is the base custom table — all 256 opcodes are initialized from scratch with
    /// compute gas tracking. Key opcode layering:
    /// - Most opcodes: `compute_gas_ext::*` (compute gas tracking only)
    /// - Block env opcodes: `volatile_data_ext::*` (gas detention)
    /// - BALANCE, EXTCODESIZE, EXTCODECOPY, EXTCODEHASH: `volatile_data_ext::*` (conditional)
    /// - SSTORE: `additional_limit_ext` → `storage_gas_ext` → `compute_gas_ext`
    /// - LOG0–LOG4: `additional_limit_ext` → `storage_gas_ext` → `compute_gas_ext`
    /// - CALL: `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext`
    /// - CREATE, CREATE2: `forward_gas_ext` → `storage_gas_ext` → `compute_gas_ext`
    /// - CALLCODE, DELEGATECALL, STATICCALL: `compute_gas_ext::*` (bug: missing `forward_gas_ext`)
    /// - SELFDESTRUCT: disabled (`control::invalid`)
    pub(super) const fn instruction_table<
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ContextTr + JournalInspectTr + ?Sized,
    >() -> [Instruction<WIRE, H>; 256] {
        use revm::bytecode::opcode::*;
        let mut table = [control::unknown as Instruction<WIRE, H>; 256];

        table[STOP as usize] = compute_gas_ext::stop;
        table[ADD as usize] = compute_gas_ext::add;
        table[MUL as usize] = compute_gas_ext::mul;
        table[SUB as usize] = compute_gas_ext::sub;
        table[DIV as usize] = compute_gas_ext::div;
        table[SDIV as usize] = compute_gas_ext::sdiv;
        table[MOD as usize] = compute_gas_ext::rem;
        table[SMOD as usize] = compute_gas_ext::smod;
        table[ADDMOD as usize] = compute_gas_ext::addmod;
        table[MULMOD as usize] = compute_gas_ext::mulmod;
        table[EXP as usize] = compute_gas_ext::exp;
        table[SIGNEXTEND as usize] = compute_gas_ext::signextend;

        table[LT as usize] = compute_gas_ext::lt;
        table[GT as usize] = compute_gas_ext::gt;
        table[SLT as usize] = compute_gas_ext::slt;
        table[SGT as usize] = compute_gas_ext::sgt;
        table[EQ as usize] = compute_gas_ext::eq;
        table[ISZERO as usize] = compute_gas_ext::iszero;
        table[AND as usize] = compute_gas_ext::bitand;
        table[OR as usize] = compute_gas_ext::bitor;
        table[XOR as usize] = compute_gas_ext::bitxor;
        table[NOT as usize] = compute_gas_ext::not;
        table[BYTE as usize] = compute_gas_ext::byte;
        table[SHL as usize] = compute_gas_ext::shl;
        table[SHR as usize] = compute_gas_ext::shr;
        table[SAR as usize] = compute_gas_ext::sar;
        table[CLZ as usize] = compute_gas_ext::clz;

        table[KECCAK256 as usize] = compute_gas_ext::keccak256;

        table[ADDRESS as usize] = compute_gas_ext::address;
        table[BALANCE as usize] = volatile_data_ext::balance;
        table[ORIGIN as usize] = compute_gas_ext::origin;
        table[CALLER as usize] = compute_gas_ext::caller;
        table[CALLVALUE as usize] = compute_gas_ext::callvalue;
        table[CALLDATALOAD as usize] = compute_gas_ext::calldataload;
        table[CALLDATASIZE as usize] = compute_gas_ext::calldatasize;
        table[CALLDATACOPY as usize] = compute_gas_ext::calldatacopy;
        table[CODESIZE as usize] = compute_gas_ext::codesize;
        table[CODECOPY as usize] = compute_gas_ext::codecopy;

        table[GASPRICE as usize] = compute_gas_ext::gasprice;
        table[EXTCODESIZE as usize] = volatile_data_ext::extcodesize;
        table[EXTCODECOPY as usize] = volatile_data_ext::extcodecopy;
        table[EXTCODEHASH as usize] = volatile_data_ext::extcodehash;
        table[RETURNDATASIZE as usize] = compute_gas_ext::returndatasize;
        table[RETURNDATACOPY as usize] = compute_gas_ext::returndatacopy;
        table[BLOCKHASH as usize] = volatile_data_ext::blockhash;
        table[COINBASE as usize] = volatile_data_ext::coinbase;
        table[TIMESTAMP as usize] = volatile_data_ext::timestamp;
        table[NUMBER as usize] = volatile_data_ext::block_number;
        table[DIFFICULTY as usize] = volatile_data_ext::difficulty;
        table[GASLIMIT as usize] = volatile_data_ext::gas_limit_opcode;
        table[CHAINID as usize] = compute_gas_ext::chainid;
        table[SELFBALANCE as usize] = compute_gas_ext::selfbalance;
        table[BASEFEE as usize] = volatile_data_ext::basefee;
        table[BLOBBASEFEE as usize] = volatile_data_ext::blobbasefee;
        table[BLOBHASH as usize] = volatile_data_ext::blobhash;

        table[POP as usize] = compute_gas_ext::pop;
        table[MLOAD as usize] = compute_gas_ext::mload;
        table[MSTORE as usize] = compute_gas_ext::mstore;
        table[MSTORE8 as usize] = compute_gas_ext::mstore8;
        table[SLOAD as usize] = compute_gas_ext::sload;
        table[SSTORE as usize] = additional_limit_ext::sstore;
        table[JUMP as usize] = compute_gas_ext::jump;
        table[JUMPI as usize] = compute_gas_ext::jumpi;
        table[PC as usize] = compute_gas_ext::pc;
        table[MSIZE as usize] = compute_gas_ext::msize;
        table[GAS as usize] = compute_gas_ext::gas;
        table[JUMPDEST as usize] = compute_gas_ext::jumpdest;
        table[TLOAD as usize] = compute_gas_ext::tload;
        table[TSTORE as usize] = compute_gas_ext::tstore;
        table[MCOPY as usize] = compute_gas_ext::mcopy;

        table[PUSH0 as usize] = compute_gas_ext::push0;
        table[PUSH1 as usize] = compute_gas_ext::push1;
        table[PUSH2 as usize] = compute_gas_ext::push2;
        table[PUSH3 as usize] = compute_gas_ext::push3;
        table[PUSH4 as usize] = compute_gas_ext::push4;
        table[PUSH5 as usize] = compute_gas_ext::push5;
        table[PUSH6 as usize] = compute_gas_ext::push6;
        table[PUSH7 as usize] = compute_gas_ext::push7;
        table[PUSH8 as usize] = compute_gas_ext::push8;
        table[PUSH9 as usize] = compute_gas_ext::push9;
        table[PUSH10 as usize] = compute_gas_ext::push10;
        table[PUSH11 as usize] = compute_gas_ext::push11;
        table[PUSH12 as usize] = compute_gas_ext::push12;
        table[PUSH13 as usize] = compute_gas_ext::push13;
        table[PUSH14 as usize] = compute_gas_ext::push14;
        table[PUSH15 as usize] = compute_gas_ext::push15;
        table[PUSH16 as usize] = compute_gas_ext::push16;
        table[PUSH17 as usize] = compute_gas_ext::push17;
        table[PUSH18 as usize] = compute_gas_ext::push18;
        table[PUSH19 as usize] = compute_gas_ext::push19;
        table[PUSH20 as usize] = compute_gas_ext::push20;
        table[PUSH21 as usize] = compute_gas_ext::push21;
        table[PUSH22 as usize] = compute_gas_ext::push22;
        table[PUSH23 as usize] = compute_gas_ext::push23;
        table[PUSH24 as usize] = compute_gas_ext::push24;
        table[PUSH25 as usize] = compute_gas_ext::push25;
        table[PUSH26 as usize] = compute_gas_ext::push26;
        table[PUSH27 as usize] = compute_gas_ext::push27;
        table[PUSH28 as usize] = compute_gas_ext::push28;
        table[PUSH29 as usize] = compute_gas_ext::push29;
        table[PUSH30 as usize] = compute_gas_ext::push30;
        table[PUSH31 as usize] = compute_gas_ext::push31;
        table[PUSH32 as usize] = compute_gas_ext::push32;

        table[DUP1 as usize] = compute_gas_ext::dup1;
        table[DUP2 as usize] = compute_gas_ext::dup2;
        table[DUP3 as usize] = compute_gas_ext::dup3;
        table[DUP4 as usize] = compute_gas_ext::dup4;
        table[DUP5 as usize] = compute_gas_ext::dup5;
        table[DUP6 as usize] = compute_gas_ext::dup6;
        table[DUP7 as usize] = compute_gas_ext::dup7;
        table[DUP8 as usize] = compute_gas_ext::dup8;
        table[DUP9 as usize] = compute_gas_ext::dup9;
        table[DUP10 as usize] = compute_gas_ext::dup10;
        table[DUP11 as usize] = compute_gas_ext::dup11;
        table[DUP12 as usize] = compute_gas_ext::dup12;
        table[DUP13 as usize] = compute_gas_ext::dup13;
        table[DUP14 as usize] = compute_gas_ext::dup14;
        table[DUP15 as usize] = compute_gas_ext::dup15;
        table[DUP16 as usize] = compute_gas_ext::dup16;

        table[SWAP1 as usize] = compute_gas_ext::swap1;
        table[SWAP2 as usize] = compute_gas_ext::swap2;
        table[SWAP3 as usize] = compute_gas_ext::swap3;
        table[SWAP4 as usize] = compute_gas_ext::swap4;
        table[SWAP5 as usize] = compute_gas_ext::swap5;
        table[SWAP6 as usize] = compute_gas_ext::swap6;
        table[SWAP7 as usize] = compute_gas_ext::swap7;
        table[SWAP8 as usize] = compute_gas_ext::swap8;
        table[SWAP9 as usize] = compute_gas_ext::swap9;
        table[SWAP10 as usize] = compute_gas_ext::swap10;
        table[SWAP11 as usize] = compute_gas_ext::swap11;
        table[SWAP12 as usize] = compute_gas_ext::swap12;
        table[SWAP13 as usize] = compute_gas_ext::swap13;
        table[SWAP14 as usize] = compute_gas_ext::swap14;
        table[SWAP15 as usize] = compute_gas_ext::swap15;
        table[SWAP16 as usize] = compute_gas_ext::swap16;

        table[LOG0 as usize] = additional_limit_ext::log::<0, _, _>;
        table[LOG1 as usize] = additional_limit_ext::log::<1, _, _>;
        table[LOG2 as usize] = additional_limit_ext::log::<2, _, _>;
        table[LOG3 as usize] = additional_limit_ext::log::<3, _, _>;
        table[LOG4 as usize] = additional_limit_ext::log::<4, _, _>;

        table[CREATE as usize] = forward_gas_ext::create;
        table[CREATE2 as usize] = forward_gas_ext::create2;
        table[CALL as usize] = forward_gas_ext::call;
        table[CALLCODE as usize] = compute_gas_ext::call_code;
        table[DELEGATECALL as usize] = compute_gas_ext::delegate_call;
        table[STATICCALL as usize] = compute_gas_ext::static_call;

        table[INVALID as usize] = compute_gas_ext::invalid;
        table[RETURN as usize] = compute_gas_ext::ret;
        table[REVERT as usize] = compute_gas_ext::revert;
        table[SELFDESTRUCT as usize] = control::invalid;

        table
    }
}

/// Call-like and create-like opcode handlers with 98/100 gas forwarding rule.
///
/// This module provides wrapper implementations for CALL, CALLCODE, DELEGATECALL, STATICCALL,
/// CREATE, and CREATE2 opcodes that enforce the 98/100 gas forwarding rule (retaining 2% of
/// remaining gas in the parent call instead of the standard 1/64).
///
/// The wrappers:
/// 1. Check for value transfer (CALL and CALLCODE only) to account for call stipend
/// 2. Call the underlying opcode implementation
/// 3. Cap the forwarded gas to 98% of the parent's remaining gas
/// 4. Preserve the call stipend (2300 gas) when value is transferred (CALL/CALLCODE only)
/// 5. Support both Call and Create frame types
pub mod forward_gas_ext {
    use super::*;

    /// Macro to wrap call-like and create-like opcodes with 98/100 gas forwarding rule.
    ///
    /// This macro generates a wrapper function that:
    /// 1. Optionally checks for value transfer (`has_transfer`) for CALL opcode
    /// 2. Calls the wrapped opcode handler
    /// 3. Caps the forwarded gas to 98/100 of the remaining gas
    /// 4. Adjusts for call stipend if value is being transferred
    /// 5. Supports both Call and Create frame types
    ///
    /// # Parameters
    /// - `$fn_name`: Name of the generated function
    /// - `$opcode_name`: String name of the opcode (for documentation)
    /// - `$wrapped_fn`: Path to the wrapped instruction implementation
    /// - `$has_transfer_logic`: Expression to determine if value is being transferred (e.g.,
    ///   `has_transfer` or `false`)
    macro_rules! wrap_gas_cap {
        ($fn_name:ident, $opcode_name:expr, $wrapped_fn:path, $has_transfer_logic:expr) => {
            #[doc = concat!("`", $opcode_name, "` opcode with 98/100 gas forwarding rule.")]
            #[inline]
            pub fn $fn_name<
                WIRE: InterpreterTypes<Stack: StackInspectTr>,
                H: HostExt + ContextTr + JournalInspectTr + ?Sized,
            >(
                context: InstructionContext<'_, H, WIRE>,
            ) {
                // Determine if there's a value transfer (only applies to CALL opcode).
                let has_transfer = $has_transfer_logic(&context);

                // Call the wrapped opcode handler.
                run_inner_instruction_or_abort!($wrapped_fn, context);

                // Cap the forwarded gas to the child call/create to the 98/100 of the remaining
                // gas.
                match context.interpreter.bytecode.action() {
                    Some(InterpreterAction::NewFrame(FrameInput::Call(call_inputs))) => {
                        // The forwarded gas to the child call should be further restricted to the
                        // 98/100 of the remaining gas. Here, we first recover the total
                        // gas left in the parent call and then cap the child call gas
                        // limit if necessary.

                        // We recover the forwarded gas to the child call from the parent call.
                        let child_gas = call_inputs.gas_limit as u128;
                        // There may be a call stipend if there is value to be transferred.
                        let transfer_gas_stipend =
                            if has_transfer { gas::CALL_STIPEND as u128 } else { 0 };
                        let forwarded_gas = child_gas - transfer_gas_stipend; // Safe from underflow

                        // Recover the remaining gas in the parent call before forwarding to the
                        // child call.
                        let parent_original_gas_left =
                            context.interpreter.gas.remaining() as u128 + forwarded_gas;

                        // Calculate the amount of gas that should be returned to the parent call
                        // under the 98/100 rule.
                        let forwarded_gas_cap =
                            parent_original_gas_left - parent_original_gas_left * 2 / 100;
                        let capped_forwarded_gas = min(forwarded_gas, forwarded_gas_cap);
                        let gas_to_return = forwarded_gas - capped_forwarded_gas; // Safe from underflow

                        // Recalculate the child gas
                        let new_child_gas = capped_forwarded_gas + transfer_gas_stipend;

                        //  Return the gas to the parent call.
                        context.interpreter.gas.erase_cost(gas_to_return as u64);

                        // Set the child call gas limit to the capped value.
                        // Note: REX4+ STORAGE_CALL_STIPEND is applied later in
                        // AdditionalLimit::before_frame_init, which owns the full
                        // stipend lifecycle (grant → compute cap → burn on return).
                        call_inputs.gas_limit = new_child_gas as u64;
                    }
                    Some(InterpreterAction::NewFrame(FrameInput::Create(create_inputs))) => {
                        // The forwarded gas to the child create should be further restricted to the
                        // 98/100 of the remaining gas. CREATE opcodes don't have a call
                        // stipend, so the logic is simpler.

                        // We recover the forwarded gas from the parent call.
                        let child_gas = create_inputs.gas_limit as u128;
                        let forwarded_gas = child_gas; // No stipend for CREATE

                        // Recover the remaining gas in the parent call before forwarding to the
                        // child create.
                        let parent_original_gas_left =
                            context.interpreter.gas.remaining() as u128 + forwarded_gas;

                        // Calculate the amount of gas that should be returned to the parent call
                        // under the 98/100 rule.
                        let forwarded_gas_cap =
                            parent_original_gas_left - parent_original_gas_left * 2 / 100;
                        let capped_forwarded_gas = min(forwarded_gas, forwarded_gas_cap);
                        let gas_to_return = forwarded_gas - capped_forwarded_gas; // Safe from underflow

                        //  Return the gas to the parent call.
                        context.interpreter.gas.erase_cost(gas_to_return as u64);

                        // Set the child create gas limit to the capped value.
                        create_inputs.gas_limit = capped_forwarded_gas as u64;
                    }
                    _ => {}
                }
            }
        };
    }

    // Helper function to check if CALL has value transfer
    #[inline]
    fn check_call_has_transfer<
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ?Sized,
    >(
        context: &InstructionContext<'_, H, WIRE>,
    ) -> bool {
        if let Some(value) = context.interpreter.stack.inspect::<2>() {
            !value.is_zero()
        } else {
            false
        }
    }

    // Helper function for opcodes without value transfer
    #[inline]
    fn no_transfer<WIRE: InterpreterTypes<Stack: StackInspectTr>, H: HostExt + ?Sized>(
        _context: &InstructionContext<'_, H, WIRE>,
    ) -> bool {
        false
    }

    wrap_gas_cap!(call, "CALL", storage_gas_ext::call, check_call_has_transfer);
    wrap_gas_cap!(call_code, "CALLCODE", storage_gas_ext::call_code, check_call_has_transfer);
    wrap_gas_cap!(delegate_call, "DELEGATECALL", storage_gas_ext::delegate_call, no_transfer);
    wrap_gas_cap!(static_call, "STATICCALL", storage_gas_ext::static_call, no_transfer);
    wrap_gas_cap!(create, "CREATE", storage_gas_ext::create::<WIRE, false, H>, no_transfer);
    wrap_gas_cap!(create2, "CREATE2", storage_gas_ext::create::<WIRE, true, H>, no_transfer);
}

/** Volatile data access opcode handlers with compute gas limit enforcement.

These custom instruction handlers override opcodes that access volatile data (block environment,
beneficiary account data, oracle contract) to lower the compute gas limit.
This prevents `DoS` attacks while allowing storage operations to continue with full transaction gas.

# Compute Gas Limit Enforcement

When volatile data is accessed:
1. The opcode executes normally (calls host method, processes data)
2. If this is the first volatile data access in the transaction:
   - The compute gas limit is lowered based on the type:
     * Block environment or beneficiary: `BLOCK_ENV_ACCESS_REMAINING_GAS` (20M compute gas)
     * Oracle contract: `ORACLE_ACCESS_REMAINING_GAS` (1M compute gas)
3. Most restrictive limit wins: If additional volatile data with different limit is accessed,
   the minimum (most restrictive) limit is applied, regardless of access order
4. All subsequent compute operations are limited by this compute gas limit
5. Storage operations (SSTORE, account creation) continue with full transaction gas

# Volatile Data Access Disable (Rex4+)

When `disableVolatileDataAccess()` is active, the handlers check **before** executing the
opcode and revert immediately if the access would be volatile.
This ensures that disabled volatile accesses do not pollute the tracker's `volatile_data_accessed`
bitmap or lower the `compute_gas_limit`.

# Two Categories of Opcodes

## Block Environment Opcodes (Always Volatile)
These opcodes ALWAYS access volatile data and apply 20M compute gas limit:
- TIMESTAMP, NUMBER, COINBASE, DIFFICULTY, GASLIMIT, BASEFEE, BLOCKHASH, BLOBBASEFEE, BLOBHASH

## Account-Accessing Opcodes (Conditionally Volatile)
These opcodes only SOMETIMES access volatile data (20M compute gas limit when volatile):
- `BALANCE(beneficiary_address)` → volatile, applies 20M compute gas limit
- `BALANCE(other_address)` → not volatile, no limit
- EXTCODESIZE/EXTCODECOPY/EXTCODEHASH → same conditional behavior

For conditional opcodes, the instruction handler peeks the target address from the stack before
executing the opcode to determine if the access would be volatile.

## Oracle SLOAD (Rex3+)
SLOAD targeting the oracle contract is volatile and applies the oracle compute gas limit.
The target address comes from `interpreter.input.target_address()` (not from the stack).
*/
pub mod volatile_data_ext {
    use super::*;

    use alloy_primitives::Address;

    use crate::{
        volatile_data_access_disabled_revert_data, VolatileDataAccessType, ORACLE_CONTRACT_ADDRESS,
    };

    /// Applies the compute gas limit from the volatile data tracker to the additional limit.
    ///
    /// This is safe to call unconditionally after any instruction: `get_compute_gas_limit()`
    /// returns `None` if no volatile data has been accessed in this transaction, and if a
    /// prior instruction already set the limit, re-applying the same value is idempotent.
    macro_rules! apply_compute_gas_limit {
        ($context:expr) => {
            let compute_gas_limit =
                $context.host.volatile_data_tracker().borrow().get_compute_gas_limit();
            if let Some(limit) = compute_gas_limit {
                $context.host.additional_limit().borrow_mut().set_compute_gas_limit(limit);
            }
        };
    }

    /// Macro to create opcode handlers for **unconditionally volatile** opcodes.
    ///
    /// These opcodes (TIMESTAMP, NUMBER, etc.) always access volatile data.
    /// The handler:
    /// 1. Checks if volatile data access is disabled (Rex4+) — if so, reverts immediately
    ///    **before** executing the opcode, avoiding any side effects on the tracker.
    /// 2. Executes the original instruction.
    /// 3. Applies the compute gas limit via `apply_compute_gas_limit!`.
    macro_rules! wrap_op_detain_gas_unconditional {
    ($fn_name:ident, $opcode_name:expr, $original_fn:path, $access_type:expr) => {
        #[doc = concat!("`", $opcode_name, "` opcode with compute gas limit enforcement on volatile data access.")]
        #[inline]
        pub fn $fn_name<WIRE: InterpreterTypes, H: HostExt + ?Sized>(
            context: InstructionContext<'_, H, WIRE>,
        ) {
            // Rex4+: revert before executing if volatile data access is disabled.
            if context.host.volatile_access_disabled() {
                context.interpreter.bytecode.set_action(InterpreterAction::new_return(
                    InstructionResult::Revert,
                    volatile_data_access_disabled_revert_data($access_type),
                    context.interpreter.gas,
                ));
                return;
            }

            run_inner_instruction_or_abort!($original_fn, context);
            apply_compute_gas_limit!(context);
        }
    };
    }

    /// Macro to create opcode handlers for **conditionally volatile** opcodes.
    ///
    /// These opcodes (BALANCE, EXTCODESIZE, EXTCODECOPY, EXTCODEHASH) are volatile only when
    /// targeting the block beneficiary address.
    /// The handler:
    /// 1. Peeks the target address from the stack (position 0) without consuming it.
    /// 2. If the target is the beneficiary and volatile access is disabled, reverts immediately
    ///    **before** executing the opcode.
    /// 3. Otherwise executes the instruction normally and applies gas detention if volatile data
    ///    was accessed.
    macro_rules! wrap_op_detain_gas_conditional {
    ($fn_name:ident, $opcode_name:expr, $original_fn:path) => {
        #[doc = concat!("`", $opcode_name, "` opcode with compute gas limit enforcement on volatile data access.")]
        #[inline]
        pub fn $fn_name<
            WIRE: InterpreterTypes<Stack: StackInspectTr>,
            H: HostExt + ContextTr + JournalInspectTr + ?Sized,
        >(
            context: InstructionContext<'_, H, WIRE>,
        ) {
            // Peek the target address from the stack to check if it's the beneficiary.
            // Rex4+: If targeting the beneficiary while volatile access is disabled, revert
            // before executing the opcode to avoid polluting the tracker.
            if let Some(addr_word) = context.interpreter.stack.inspect::<0>() {
                let target: Address = addr_word.into_address();
                let beneficiary = context.host.beneficiary_address();
                if target == beneficiary && context.host.volatile_access_disabled() {
                    context.interpreter.bytecode.set_action(InterpreterAction::new_return(
                        InstructionResult::Revert,
                        volatile_data_access_disabled_revert_data(
                            VolatileDataAccessType::Beneficiary,
                        ),
                        context.interpreter.gas,
                    ));
                    return;
                }
            }

            run_inner_instruction_or_abort!($original_fn, context);
            apply_compute_gas_limit!(context);
        }
    };
    }

    // Unconditional volatile opcodes — always access volatile data, no stack inspection needed.
    wrap_op_detain_gas_unconditional!(
        timestamp,
        "TIMESTAMP",
        compute_gas_ext::timestamp,
        VolatileDataAccessType::Timestamp
    );
    wrap_op_detain_gas_unconditional!(
        block_number,
        "NUMBER",
        compute_gas_ext::number,
        VolatileDataAccessType::BlockNumber
    );
    wrap_op_detain_gas_unconditional!(
        difficulty,
        "DIFFICULTY",
        compute_gas_ext::difficulty,
        VolatileDataAccessType::Difficulty
    );
    wrap_op_detain_gas_unconditional!(
        gas_limit_opcode,
        "GASLIMIT",
        compute_gas_ext::gaslimit,
        VolatileDataAccessType::GasLimit
    );
    wrap_op_detain_gas_unconditional!(
        basefee,
        "BASEFEE",
        compute_gas_ext::basefee,
        VolatileDataAccessType::BaseFee
    );
    wrap_op_detain_gas_unconditional!(
        coinbase,
        "COINBASE",
        compute_gas_ext::coinbase,
        VolatileDataAccessType::Coinbase
    );
    wrap_op_detain_gas_unconditional!(
        blockhash,
        "BLOCKHASH",
        compute_gas_ext::blockhash,
        VolatileDataAccessType::BlockHash
    );
    wrap_op_detain_gas_unconditional!(
        blobbasefee,
        "BLOBBASEFEE",
        compute_gas_ext::blobbasefee,
        VolatileDataAccessType::BlobBaseFee
    );
    wrap_op_detain_gas_unconditional!(
        blobhash,
        "BLOBHASH",
        compute_gas_ext::blobhash,
        VolatileDataAccessType::BlobHash
    );

    // Conditional volatile opcodes — volatile only when targeting the block beneficiary.
    wrap_op_detain_gas_conditional!(balance, "BALANCE", compute_gas_ext::balance);
    wrap_op_detain_gas_conditional!(extcodesize, "EXTCODESIZE", compute_gas_ext::extcodesize);
    wrap_op_detain_gas_conditional!(extcodecopy, "EXTCODECOPY", compute_gas_ext::extcodecopy);
    wrap_op_detain_gas_conditional!(extcodehash, "EXTCODEHASH", compute_gas_ext::extcodehash);
    wrap_op_detain_gas_conditional!(selfdestruct, "SELFDESTRUCT", compute_gas_ext::selfdestruct);

    // REX5 SELFDESTRUCT outer wrapper: guard outermost, then the
    // `storage_gas_ext::selfdestruct` layer (new-account storage gas charge),
    // then `compute_gas_ext::selfdestruct`.
    wrap_op_detain_gas_conditional!(
        selfdestruct_rex5,
        "SELFDESTRUCT",
        super::storage_gas_ext::selfdestruct
    );

    /// `SELFBALANCE` opcode with compute gas limit enforcement on volatile data access.
    ///
    /// SELFBALANCE is conditionally volatile when the current contract is the beneficiary.
    /// Unlike the other beneficiary-conditional opcodes (BALANCE, EXTCODESIZE, etc.),
    /// the target comes from `interpreter.input.target_address()` (the executing contract),
    /// not from a stack operand, so `wrap_op_detain_gas_conditional` cannot be reused.
    #[inline]
    pub fn selfbalance<WIRE: InterpreterTypes, H: HostExt + ?Sized>(
        context: InstructionContext<'_, H, WIRE>,
    ) {
        let target = context.interpreter.input.target_address();
        let beneficiary = context.host.beneficiary_address();
        if target == beneficiary && context.host.volatile_access_disabled() {
            context.interpreter.bytecode.set_action(InterpreterAction::new_return(
                InstructionResult::Revert,
                volatile_data_access_disabled_revert_data(VolatileDataAccessType::Beneficiary),
                context.interpreter.gas,
            ));
            return;
        }

        run_inner_instruction_or_abort!(compute_gas_ext::selfbalance, context);
        apply_compute_gas_limit!(context);
    }

    /// `SLOAD` opcode with compute gas limit enforcement on volatile data access.
    ///
    /// SLOAD is conditionally volatile when targeting the oracle contract.
    /// Unlike the beneficiary-conditional opcodes, the target address comes from
    /// `interpreter.input.target_address()` (the current contract), not from the stack.
    ///
    /// The handler checks if the SLOAD targets the oracle contract and volatile access is
    /// disabled — if so, reverts before executing the instruction.
    #[inline]
    pub fn sload<WIRE: InterpreterTypes, H: HostExt + ?Sized>(
        context: InstructionContext<'_, H, WIRE>,
    ) {
        // Rex4+: If SLOAD targets the oracle contract and volatile access is disabled,
        // revert before executing to avoid polluting the tracker.
        let target = context.interpreter.input.target_address();
        if target == ORACLE_CONTRACT_ADDRESS && context.host.volatile_access_disabled() {
            context.interpreter.bytecode.set_action(InterpreterAction::new_return(
                InstructionResult::Revert,
                volatile_data_access_disabled_revert_data(VolatileDataAccessType::Oracle),
                context.interpreter.gas,
            ));
            return;
        }

        run_inner_instruction_or_abort!(compute_gas_ext::sload, context);
        apply_compute_gas_limit!(context);
    }

    /// Macro to create opcode handlers for **conditionally volatile CALL-like** opcodes.
    ///
    /// These opcodes (CALL, STATICCALL, DELEGATECALL, CALLCODE) are volatile only when
    /// targeting the block beneficiary address.
    /// The handler:
    /// 1. Peeks the target address from the stack (position 1) without consuming it.
    /// 2. If the target is the beneficiary and volatile access is disabled, reverts immediately
    ///    **before** executing the opcode to avoid polluting the tracker via
    ///    `load_account_delegated`.
    /// 3. Otherwise delegates to the existing `forward_gas_ext` handler.
    macro_rules! wrap_call_volatile_check {
    ($fn_name:ident, $opcode_name:expr, $inner_fn:path) => {
        #[doc = concat!("`", $opcode_name, "` opcode with volatile data access disabled check for beneficiary.")]
        #[inline]
        pub fn $fn_name<
            WIRE: InterpreterTypes<Stack: StackInspectTr>,
            H: HostExt + ContextTr + JournalInspectTr + ?Sized,
        >(
            context: InstructionContext<'_, H, WIRE>,
        ) {
            // Peek the target address from the stack (position 1 for CALL-like opcodes:
            // stack layout is [gas_limit, to, ...]).
            // Rex4+: If targeting the beneficiary while volatile access is disabled, revert
            // before executing the opcode to avoid polluting the tracker.
            if let Some(addr_word) = context.interpreter.stack.inspect::<1>() {
                let target: Address = addr_word.into_address();
                let beneficiary = context.host.beneficiary_address();
                if target == beneficiary && context.host.volatile_access_disabled() {
                    context.interpreter.bytecode.set_action(InterpreterAction::new_return(
                        InstructionResult::Revert,
                        volatile_data_access_disabled_revert_data(
                            VolatileDataAccessType::Beneficiary,
                        ),
                        context.interpreter.gas,
                    ));
                    return;
                }
            }

            // Delegate to the existing forward_gas_ext handler via reborrow so that
            // `context` remains usable for `apply_compute_gas_limit!` afterward.
            {
                let ctx = InstructionContext::<'_, H, WIRE> {
                    interpreter: &mut *context.interpreter,
                    host: &mut *context.host,
                };
                $inner_fn(ctx);
            }

            // Propagate the detained compute gas limit if the CALL triggered beneficiary
            // access (via `host.load_account_delegated()` inside the CALL handler).
            // `apply_compute_gas_limit!` only touches the tracker and `AdditionalLimit`,
            // not interpreter state, so it is safe in any interpreter state (including
            // `NewFrame` after a successful CALL).
            apply_compute_gas_limit!(context);
        }
    };
    }

    // Conditionally volatile CALL-like opcodes — volatile only when targeting the block
    // beneficiary. These wrap forward_gas_ext handlers with a pre-execution beneficiary check.
    wrap_call_volatile_check!(call, "CALL", forward_gas_ext::call);
    wrap_call_volatile_check!(static_call, "STATICCALL", forward_gas_ext::static_call);
    wrap_call_volatile_check!(delegate_call, "DELEGATECALL", forward_gas_ext::delegate_call);
    wrap_call_volatile_check!(call_code, "CALLCODE", forward_gas_ext::call_code);
}

/// Extends opcodes with additional limit (kv update limit, data limit, etc.) enforcement.
pub mod additional_limit_ext {
    use super::*;

    /// `SSTORE` opcode implementation with data size and KV update limit enforcement.
    ///
    /// This wrapper adds limit tracking on top of [`storage_gas_ext::sstore`], which handles
    /// compute gas tracking and storage gas costs.
    ///
    /// # Data Size and KV Update Tracking
    ///
    /// When first writing non-zero value to originally-zero slot:
    /// - Adds 40 bytes to transaction data size
    /// - Adds 1 KV update count
    ///
    /// # Limit Enforcement
    ///
    /// Halts with `OutOfGas` when data (3.125 MB) or KV (1,000) limits exceeded.
    ///
    /// # Refund Logic
    ///
    /// Refunds data/KV when slot reset to original value.
    pub fn sstore<
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ContextTr + JournalInspectTr + ?Sized,
    >(
        context: InstructionContext<'_, H, WIRE>,
    ) {
        // Load storage slot values before executing the instruction
        let target_address = context.interpreter.input.target_address();
        let Some(index) = context.interpreter.stack.inspect::<0>() else {
            context.interpreter.halt(InstructionResult::StackUnderflow);
            return;
        };
        let mega_spec = context.host.spec_id();
        let Ok(slot) = context.host.inspect_storage(mega_spec, target_address, index) else {
            context.interpreter.halt(InstructionResult::FatalExternalError);
            return;
        };
        let (original_value, present_value) = (slot.original_value(), slot.present_value());
        let Some(new_value) = context.interpreter.stack.inspect::<1>() else {
            context.interpreter.halt(InstructionResult::StackUnderflow);
            return;
        };
        let loaded_data = SStoreResult { original_value, present_value, new_value };

        // Execute the original SSTORE instruction
        run_inner_instruction_or_abort!(storage_gas_ext::sstore, context);

        // KV update bomb and data bomb (only when first writing non-zero value to originally zero
        // slot): check if the number of key-value updates or the total data size will exceed the
        // limit, if so, halt.
        let additional_limit = context.host.additional_limit();
        let mut additional_limit = additional_limit.borrow_mut();
        if !additional_limit.on_sstore(target_address, index, &loaded_data) {
            context.interpreter.halt(additional_limit.exceeding_instruction_result());
        }
    }

    /// `LOG` opcode implementation with data size limit enforcement.
    ///
    /// This wrapper adds data limit tracking on top of [`storage_gas_ext::log`], which handles
    /// compute gas tracking and storage gas costs.
    ///
    /// # Data Size Limit Enforcement
    ///
    /// After log emission, checks if total transaction data size exceeds `TX_DATA_LIMIT` (3.125
    /// MB). Halts when data limit exceeded.
    pub fn log<
        const N: usize,
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ContextTr + JournalInspectTr + ?Sized,
    >(
        context: InstructionContext<'_, H, WIRE>,
    ) {
        // Get the log data length before executing the instruction
        let Some(len) = context.interpreter.stack.inspect::<1>() else {
            context.interpreter.halt(InstructionResult::StackUnderflow);
            return;
        };
        let len = as_usize_or_fail!(context.interpreter, len);

        // Execute the original LOG instruction
        run_inner_instruction_or_abort!(storage_gas_ext::log::<N, WIRE, H>, context);

        // Record the size of the log topics and data. If the total data size exceeds the limit, we
        // halt.
        let additional_limit = context.host.additional_limit();
        let mut additional_limit = additional_limit.borrow_mut();
        if !additional_limit.on_log(N as u64, len as u64) {
            context.interpreter.halt(additional_limit.exceeding_instruction_result());
        }
    }
}

/// Extends opcodes with storage gas cost on top of `compute_gas_ext`.
pub mod storage_gas_ext {
    use super::*;
    use alloy_primitives::Address;

    /// Address-selector for opcodes where the storage account is the stack `to` address (e.g.
    /// CALL).
    fn storage_addr_from_to(_mega_spec: MegaSpecId, _current: Address, to: Address) -> Address {
        to
    }

    /// Address-selector for CALLCODE: Rex5+ uses the current frame's address because CALLCODE
    /// executes borrowed code in the caller's own storage context; pre-Rex5 preserves the frozen
    /// behavior of metering against the code-source (stack `to`).
    fn storage_addr_for_callcode(mega_spec: MegaSpecId, current: Address, to: Address) -> Address {
        if mega_spec.is_enabled(MegaSpecId::REX5) {
            current
        } else {
            to
        }
    }

    /// Macro to charge storage gas for new account creation before calling the wrapped instruction.
    ///
    /// This macro generates a wrapper function that:
    /// 1. Inspects the target address (stack position 1) and value (stack position 2)
    /// 2. Resolves the storage account address via `$select_addr`
    /// 3. Checks if the storage account is empty and value transfer is non-zero
    /// 4. Charges storage gas for new account creation if applicable
    /// 5. Calls the wrapped instruction implementation
    ///
    /// # Call Opcode Behavior
    ///
    /// The generated `CALL` and `CALLCODE` implementations add:
    ///
    /// **Dynamic New Account Gas**: When calling empty account with value transfer:
    /// - Base cost 2,000,000 gas, multiplied by `bucket_capacity / MIN_BUCKET_SIZE`
    ///
    /// # Parameters
    /// - `$fn_name`: Name of the generated function
    /// - `$opcode_name`: String name of the opcode (for documentation)
    /// - `$wrapped_fn`: Path to the wrapped instruction implementation
    /// - `$has_transfer_logic`: `true` if the opcode can transfer value (inspects stack position 2)
    /// - `$select_addr` (optional): Path to a `fn(MegaSpecId, current: Address, to: Address) ->
    ///   Address` function that returns the address to check for emptiness and charge
    ///   `new_account_storage_gas` against. `current` is the current frame's address; `to` is the
    ///   stack position-1 address. Defaults to [`storage_addr_from_to`].
    macro_rules! wrap_call_with_storage_gas {
        ($fn_name:ident, $opcode_name:expr, $wrapped_fn:path, $has_transfer_logic:expr) => {
            wrap_call_with_storage_gas!(
                $fn_name,
                $opcode_name,
                $wrapped_fn,
                $has_transfer_logic,
                storage_addr_from_to
            );
        };
        ($fn_name:ident, $opcode_name:expr, $wrapped_fn:path, $has_transfer_logic:expr, $select_addr:path) => {
            #[doc = concat!("`", $opcode_name, "` opcode implementation modified from `revm` with compute gas tracking and dynamically-scaled storage gas costs.")]
            pub fn $fn_name<
                WIRE: InterpreterTypes<Stack: StackInspectTr>,
                H: HostExt + ContextTr + JournalInspectTr + ?Sized,
            >(
                context: InstructionContext<'_, H, WIRE>,
            ) {
                let spec = context.interpreter.runtime_flag.spec_id();
                let Some(to) = context.interpreter.stack.inspect::<1>() else {
                    context.interpreter.halt(InstructionResult::StackUnderflow);
                    return;
                };
                let to = to.into_address();
                let mega_spec = context.host.spec_id();
                let current_address = context.interpreter.input.target_address();
                let storage_address = $select_addr(mega_spec, current_address, to);
                let Ok(storage_account) = (if mega_spec.is_enabled(MegaSpecId::REX5) {
                    context.host.inspect_account(storage_address, false)
                } else {
                    context.host.inspect_account_delegated(mega_spec, storage_address)
                }) else {
                    context.interpreter.halt(InstructionResult::FatalExternalError);
                    return;
                };
                let is_empty = storage_account.state_clear_aware_is_empty(spec);
                let has_transfer = if $has_transfer_logic {
                    let Some(value) = context.interpreter.stack.inspect::<2>() else {
                        context.interpreter.halt(InstructionResult::StackUnderflow);
                        return;
                    };
                    !value.is_zero()
                } else {
                    false
                };
                // Charge additional storage gas cost for creating a new account.
                // REX5 drains the storage stipend allowance first; pre-REX5 returns 0.
                if is_empty && has_transfer {
                    let Some(new_account_storage_gas) =
                        context.host.new_account_storage_gas(storage_address)
                    else {
                        context.interpreter.halt(InstructionResult::FatalExternalError);
                        return;
                    };
                    let drained = context
                        .host
                        .additional_limit()
                        .borrow_mut()
                        .try_consume_storage_stipend(new_account_storage_gas);
                    gas!(context.interpreter, new_account_storage_gas - drained);
                }

                // Call the original instruction
                run_inner_instruction_or_abort!($wrapped_fn, context);
            }
        };
    }

    wrap_call_with_storage_gas!(call, "CALL", compute_gas_ext::call, true);
    wrap_call_with_storage_gas!(
        delegate_call,
        "DELEGATECALL",
        compute_gas_ext::delegate_call,
        false
    );
    wrap_call_with_storage_gas!(static_call, "STATICCALL", compute_gas_ext::static_call, false);
    wrap_call_with_storage_gas!(
        call_code,
        "CALLCODE",
        compute_gas_ext::call_code,
        true,
        storage_addr_for_callcode
    );

    /// `CREATE`/`CREATE2` opcode implementation modified from `revm` with compute gas tracking and
    /// dynamically-scaled storage gas costs.
    ///
    /// # Differences from the standard EVM
    ///
    /// 1. **Dynamic New Account Gas**: Additional storage gas for new account creation:
    ///    - Base cost 2,000,000 gas, multiplied by `bucket_capacity / MIN_BUCKET_SIZE`
    ///
    /// # Assumptions
    ///
    /// This alternative implementation of `CREATE`/`CREATE2` is only used when the `MINI_REX` spec
    /// is enabled, so we can safely assume that all features before and including `MINI_REX`
    /// are enabled.
    pub fn create<
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        const IS_CREATE2: bool,
        H: HostExt + ContextTr + JournalInspectTr + ?Sized,
    >(
        context: InstructionContext<'_, H, WIRE>,
    ) {
        let spec = context.host.spec_id();

        // The current execution contract (the caller)
        let creator_address = context.interpreter.input.target_address();
        // Load the creator account without marking it warm (it is already warm since the creator
        // must have been warmed when the current frame begins).
        // REX5+: use non-delegating inspection to get the authority's own state.
        let Ok(creator) = (if spec.is_enabled(MegaSpecId::REX5) {
            context.host.inspect_account(creator_address, false)
        } else {
            context.host.inspect_account_delegated(spec, creator_address)
        }) else {
            context.interpreter.halt(InstructionResult::FatalExternalError);
            return;
        };

        // Calculate the created address
        let mut resize_gas: u64 = 0;
        let is_rex5_enabled = spec.is_enabled(MegaSpecId::REX5);
        let created_address = if IS_CREATE2 {
            let Some(initcode_offset) = context.interpreter.stack.inspect::<1>() else {
                context.interpreter.halt(InstructionResult::StackUnderflow);
                return;
            };
            let Some(initcode_len) = context.interpreter.stack.inspect::<2>() else {
                context.interpreter.halt(InstructionResult::StackUnderflow);
                return;
            };
            // REX5+: validate the salt operand before running `resize_memory!` and the
            // copy / keccak block, so a missing salt halts with `StackUnderflow` without
            // performing the expensive memory work that the trailing late-recorded
            // `resize_gas` path would otherwise under-count. Pre-REX5 keeps the original
            // "resize first, salt last" order for replay parity.
            let rex5_salt = if is_rex5_enabled {
                let Some(salt) = context.interpreter.stack.inspect::<3>() else {
                    context.interpreter.halt(InstructionResult::StackUnderflow);
                    return;
                };
                Some(salt)
            } else {
                None
            };

            // REX5+: when `initcode_len == 0`, mirror canonical revm CREATE2 — ignore
            // the offset entirely (no conversion, no memory expansion, no slice, no
            // keccak) and use `KECCAK_EMPTY` as the initcode hash. Observing offset on
            // `len == 0` would (a) halt a valid `len=0, offset=U256::MAX` CREATE2
            // inside `as_usize_or_fail!`, and (b) over-charge memory-expansion EVM gas
            // + `resize_gas` compute gas for `len=0, offset=large_finite`.
            // Pre-REX5 keeps the "observe offset, resize, slice, hash" sequence
            // verbatim for replay parity.
            let initcode_hash = if is_rex5_enabled && initcode_len.is_zero() {
                KECCAK_EMPTY
            } else {
                let initcode_offset = as_usize_or_fail!(context.interpreter, initcode_offset);
                let initcode_len = as_usize_or_fail!(context.interpreter, initcode_len);
                // Expand memory before slicing so the read can never go out of bounds. The
                // canonical CREATE2 path called below also calls `resize_memory!`, which is
                // a no-op once memory is already sized to fit the requested slice.
                //
                // Pre-REX5 records the expansion gas into the compute_gas tracker only
                // AFTER the inner CREATE2 returns successfully (see end of function);
                // preserved for replay parity. REX5+ records it immediately below so
                // storage-gas OOG / inner-CREATE2 failure cannot bypass the recording.
                let gas_before_resize = context.interpreter.gas.remaining();
                resize_memory!(context.interpreter, initcode_offset, initcode_len);
                resize_gas = gas_before_resize.saturating_sub(context.interpreter.gas.remaining());

                // REX5+: record resize gas into the compute_gas tracker immediately to
                // align its timing with revm's EVM-gas debit (already taken inside
                // `resize_memory!`). Zero `resize_gas` afterwards so the trailing
                // late-record block (kept for pre-REX5) does not double-count under REX5.
                if is_rex5_enabled && resize_gas > 0 {
                    let mut additional_limit = context.host.additional_limit().borrow_mut();
                    compute_gas!(context.interpreter, additional_limit, resize_gas);
                    resize_gas = 0;
                }

                let code = Bytes::copy_from_slice(
                    context.interpreter.memory.slice_len(initcode_offset, initcode_len).as_ref(),
                );
                keccak256(&code)
            };

            let salt = if let Some(s) = rex5_salt {
                s
            } else {
                let Some(salt) = context.interpreter.stack.inspect::<3>() else {
                    context.interpreter.halt(InstructionResult::StackUnderflow);
                    return;
                };
                salt
            };

            creator_address.create2(salt.to_be_bytes(), initcode_hash)
        } else {
            creator_address.create(creator.info.nonce)
        };

        // Charge storage gas cost for creating a new contract
        let create_contract_storage_gas = if spec.is_enabled(MegaSpecId::REX) {
            // Rex spec distinguishes between contract creation and account creation.
            context.host.create_contract_storage_gas(created_address)
        } else {
            // Mini-Rex spec does not distinguish between contract creation and account creation.
            context.host.new_account_storage_gas(created_address)
        };
        let Some(create_contract_storage_gas) = create_contract_storage_gas else {
            context.interpreter.halt(InstructionResult::FatalExternalError);
            return;
        };
        // REX5 drains the storage stipend allowance first; pre-REX5 returns 0.
        let drained = context
            .host
            .additional_limit()
            .borrow_mut()
            .try_consume_storage_stipend(create_contract_storage_gas);
        gas!(context.interpreter, create_contract_storage_gas - drained);

        // Call the original create instruction
        if IS_CREATE2 {
            run_inner_instruction_or_abort!(compute_gas_ext::create2, context);
        } else {
            run_inner_instruction_or_abort!(compute_gas_ext::create, context);
        }

        // Pre-REX5 late-record path for the CREATE2 initcode memory-expansion gas.
        // Preserved verbatim for replay parity: pre-REX5 keeps the original "skip on inner
        // error" semantics where storage-gas OOG and inner-CREATE2 failure both skip this
        // recording. REX5+ already recorded `resize_gas` above (and zeroed it), so this
        // branch is a no-op under REX5.
        if resize_gas > 0 {
            let mut additional_limit = context.host.additional_limit().borrow_mut();
            compute_gas!(context.interpreter, additional_limit, resize_gas);
        }
    }

    /// `LOG` opcode implementation modified from `revm` with compute gas tracking, increased
    /// storage gas costs, and data size limit enforcement.
    ///
    /// # Differences from the standard EVM
    ///
    /// 1. **Storage Gas Costs**: Additional storage gas charged for log storage:
    ///    - Topic storage: 3,750 gas per topic (10x standard topic cost)
    ///    - Data storage: 80 gas per byte (10x standard data cost)
    ///
    /// # Assumptions
    ///
    /// This alternative implementation of `LOG` is only used when the `MINI_REX` spec is enabled.
    pub fn log<
        const N: usize,
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ?Sized,
    >(
        context: InstructionContext<'_, H, WIRE>,
    ) {
        let Some(len) = context.interpreter.stack.inspect::<1>() else {
            context.interpreter.halt(InstructionResult::StackUnderflow);
            return;
        };
        let len = as_usize_or_fail!(context.interpreter, len);

        // Charge storage gas cost for log topics and data before instruction execution.
        // REX5 drains the allowance on the `Some(amount)` arm; the `None` (overflow) arm
        // is passed through unchanged to preserve the OOG halt.
        let log_storage_cost = {
            let topic_cost = constants::mini_rex::LOG_TOPIC_STORAGE_GAS.checked_mul(N as u64);
            let data_cost = constants::mini_rex::LOG_DATA_STORAGE_GAS.checked_mul(len as u64);
            topic_cost.and_then(|topic| data_cost.and_then(|cost| cost.checked_add(topic)))
        };
        let log_storage_cost = log_storage_cost.map(|amount| {
            let drained =
                context.host.additional_limit().borrow_mut().try_consume_storage_stipend(amount);
            amount - drained
        });
        gas_or_fail!(context.interpreter, log_storage_cost);

        // Execute the original LOG instruction.
        match N {
            0 => {
                run_inner_instruction_or_abort!(compute_gas_ext::log0, context);
            }
            1 => {
                run_inner_instruction_or_abort!(compute_gas_ext::log1, context);
            }
            2 => {
                run_inner_instruction_or_abort!(compute_gas_ext::log2, context);
            }
            3 => {
                run_inner_instruction_or_abort!(compute_gas_ext::log3, context);
            }
            4 => {
                run_inner_instruction_or_abort!(compute_gas_ext::log4, context);
            }
            _ => {
                context.interpreter.halt(InstructionResult::InvalidFEOpcode);
            }
        }
    }

    /// `SSTORE` opcode implementation modified from `revm` with compute gas tracking and
    /// dynamically-scaled storage gas costs.
    ///
    /// # Differences from the standard EVM
    ///
    /// 1. **Dynamic Storage Gas**: Additional storage gas ONLY when setting originally-zero slot to
    ///    non-zero:
    ///    - Base cost 2,000,000 gas, multiplied by `bucket_capacity / MIN_BUCKET_SIZE`
    ///    - Not charged for updating already-non-zero slots or resetting to zero
    ///
    /// # Assumptions
    ///
    /// This alternative implementation of `SSTORE` is only used when the `MINI_REX` spec is
    /// enabled, so we can safely assume that all features before and including Mini-Rex are
    /// enabled.
    pub fn sstore<
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ContextTr + JournalInspectTr + ?Sized,
    >(
        context: InstructionContext<'_, H, WIRE>,
    ) {
        // The address to the underlying execution contract state
        let target_address = context.interpreter.input.target_address();
        // The storage slot to write
        let Some(index) = context.interpreter.stack.inspect::<0>() else {
            context.interpreter.halt(InstructionResult::StackUnderflow);
            return;
        };
        // The storage slot values
        let mega_spec = context.host.spec_id();
        let Ok(slot) = context.host.inspect_storage(mega_spec, target_address, index) else {
            context.interpreter.halt(InstructionResult::FatalExternalError);
            return;
        };
        let (original_value, present_value) = (slot.original_value(), slot.present_value());
        let Some(new_value) = context.interpreter.stack.inspect::<1>() else {
            context.interpreter.halt(InstructionResult::StackUnderflow);
            return;
        };

        // Charge storage gas cost before the instruction is executed.
        // REX5 drains the storage stipend allowance first; pre-REX5 returns 0.
        if original_value.is_zero() && present_value.is_zero() && !new_value.is_zero() {
            let Some(sstore_set_storage_gas) =
                context.host.sstore_set_storage_gas(target_address, index)
            else {
                context.interpreter.halt(InstructionResult::FatalExternalError);
                return;
            };
            let drained = context
                .host
                .additional_limit()
                .borrow_mut()
                .try_consume_storage_stipend(sstore_set_storage_gas);
            gas!(context.interpreter, sstore_set_storage_gas - drained);
        }

        // Execute the original SSTORE instruction
        run_inner_instruction_or_abort!(compute_gas_ext::sstore, context);
    }

    /// `SELFDESTRUCT` opcode implementation with storage gas metering for
    /// new beneficiary account creation (REX5+).
    ///
    /// When SELFDESTRUCT sends remaining balance to an empty beneficiary, charges:
    /// - Storage gas for new account creation (dynamic bucket-based cost)
    /// - Data size (+40 for account info write)
    /// - KV update (+1)
    /// - State growth (+1)
    ///
    /// This wrapper sits between `volatile_data_ext` and `compute_gas_ext` in the
    /// REX5 SELFDESTRUCT dispatch chain
    /// (`volatile_data_ext::selfdestruct_rex5` → `storage_gas_ext::selfdestruct`
    /// → `compute_gas_ext::selfdestruct`), matching the layering used by SSTORE
    /// and LOG. The beneficiary-volatile guard runs in the outer
    /// `volatile_data_ext::selfdestruct_rex5` ahead of any side effects below.
    pub fn selfdestruct<
        WIRE: InterpreterTypes<Stack: StackInspectTr>,
        H: HostExt + ContextTr + JournalInspectTr + ?Sized,
    >(
        context: InstructionContext<'_, H, WIRE>,
    ) {
        let eth_spec = context.interpreter.runtime_flag.spec_id();

        // Peek beneficiary address from stack (SELFDESTRUCT uses stack position 0)
        let Some(target) = context.interpreter.stack.inspect::<0>() else {
            context.interpreter.halt(InstructionResult::StackUnderflow);
            return;
        };
        let target = target.into_address();

        // Use non-delegating inspection (REX5+)
        let Ok(target_account) = context.host.inspect_account(target, false) else {
            context.interpreter.halt(InstructionResult::FatalExternalError);
            return;
        };
        let is_empty = target_account.state_clear_aware_is_empty(eth_spec);

        // Check if caller has balance (value will be transferred to beneficiary)
        let caller = context.interpreter.input.target_address();
        let Ok(caller_account) = context.host.inspect_account(caller, false) else {
            context.interpreter.halt(InstructionResult::FatalExternalError);
            return;
        };
        let has_value = !caller_account.info.balance.is_zero();

        if is_empty && has_value {
            // Charge storage gas for creating a new account.
            // REX5 drains the storage stipend allowance first; pre-REX5 returns 0.
            let Some(cost) = context.host.new_account_storage_gas(target) else {
                context.interpreter.halt(InstructionResult::FatalExternalError);
                return;
            };
            let drained =
                context.host.additional_limit().borrow_mut().try_consume_storage_stipend(cost);
            gas!(context.interpreter, cost - drained);

            // Record resource usage for new beneficiary account
            context.host.additional_limit().borrow_mut().on_selfdestruct_new_account();
        }

        // Delegate to compute_gas_ext::selfdestruct (the volatile-disabled guard
        // ran in the outer `volatile_data_ext::selfdestruct_rex5` wrapper).
        run_inner_instruction_or_abort!(compute_gas_ext::selfdestruct, context);
    }
}

/// Compute gas recording implementation. TODO: add more doc
pub mod compute_gas_ext {
    use super::*;

    /// Macro to wrap the original instruction implementation with compute gas tracking.
    macro_rules! wrap_op_compute_gas {
        ($fn_name:ident, $opcode_name:expr, $original_fn:path) => {
            #[doc = concat!("`", $opcode_name, "` opcode with compute gas tracking.")]
            #[inline]
            pub fn $fn_name<WIRE: InterpreterTypes, H: HostExt + ?Sized>(
                context: InstructionContext<'_, H, WIRE>,
            ) {
                let gas_before = context.interpreter.gas.remaining();

                // Call the original instruction
                run_inner_instruction_or_abort!($original_fn, context);

                let mut gas_used = gas_before.saturating_sub(context.interpreter.gas.remaining());
                // Subtract the gas forwarded to the child. REX5 excludes the revm-side
                // `CALL_STIPEND` (added by value-transferring CALL/CALLCODE without
                // deducting from the parent) so parent compute-gas is not under-counted.
                // Pre-REX5 keeps the legacy raw-`gas_limit` subtraction for replay parity.
                match context.interpreter.bytecode.action() {
                    Some(InterpreterAction::NewFrame(FrameInput::Call(call_inputs))) => {
                        let stipend_from_revm = if context
                            .host
                            .spec_id()
                            .is_enabled(MegaSpecId::REX5) &&
                            matches!(call_inputs.scheme, CallScheme::Call | CallScheme::CallCode) &&
                            call_inputs.transfers_value()
                        {
                            gas::CALL_STIPEND
                        } else {
                            0
                        };
                        let parent_contributed =
                            call_inputs.gas_limit.saturating_sub(stipend_from_revm);
                        gas_used = gas_used.saturating_sub(parent_contributed);
                    }
                    Some(InterpreterAction::NewFrame(FrameInput::Create(create_inputs))) => {
                        gas_used = gas_used.saturating_sub(create_inputs.gas_limit);
                    }
                    _ => {}
                }
                let mut additional_limit = context.host.additional_limit().borrow_mut();
                compute_gas!(context.interpreter, additional_limit, gas_used);
            }
        };
    }

    wrap_op_compute_gas!(stop, "STOP", instructions::control::stop);
    wrap_op_compute_gas!(add, "ADD", instructions::arithmetic::add);
    wrap_op_compute_gas!(mul, "MUL", instructions::arithmetic::mul);
    wrap_op_compute_gas!(sub, "SUB", instructions::arithmetic::sub);
    wrap_op_compute_gas!(div, "DIV", instructions::arithmetic::div);
    wrap_op_compute_gas!(sdiv, "SDIV", instructions::arithmetic::sdiv);
    wrap_op_compute_gas!(rem, "MOD", instructions::arithmetic::rem);
    wrap_op_compute_gas!(smod, "SMOD", instructions::arithmetic::smod);
    wrap_op_compute_gas!(addmod, "ADDMOD", instructions::arithmetic::addmod);
    wrap_op_compute_gas!(mulmod, "MULMOD", instructions::arithmetic::mulmod);
    wrap_op_compute_gas!(exp, "EXP", instructions::arithmetic::exp);
    wrap_op_compute_gas!(signextend, "SIGNEXTEND", instructions::arithmetic::signextend);

    wrap_op_compute_gas!(lt, "LT", instructions::bitwise::lt);
    wrap_op_compute_gas!(gt, "GT", instructions::bitwise::gt);
    wrap_op_compute_gas!(slt, "SLT", instructions::bitwise::slt);
    wrap_op_compute_gas!(sgt, "SGT", instructions::bitwise::sgt);
    wrap_op_compute_gas!(eq, "EQ", instructions::bitwise::eq);
    wrap_op_compute_gas!(iszero, "ISZERO", instructions::bitwise::iszero);
    wrap_op_compute_gas!(bitand, "AND", instructions::bitwise::bitand);
    wrap_op_compute_gas!(bitor, "OR", instructions::bitwise::bitor);
    wrap_op_compute_gas!(bitxor, "XOR", instructions::bitwise::bitxor);
    wrap_op_compute_gas!(not, "NOT", instructions::bitwise::not);
    wrap_op_compute_gas!(byte, "BYTE", instructions::bitwise::byte);
    wrap_op_compute_gas!(shl, "SHL", instructions::bitwise::shl);
    wrap_op_compute_gas!(shr, "SHR", instructions::bitwise::shr);
    wrap_op_compute_gas!(sar, "SAR", instructions::bitwise::sar);
    wrap_op_compute_gas!(clz, "CLZ", instructions::bitwise::clz);

    wrap_op_compute_gas!(keccak256, "KECCAK256", instructions::system::keccak256);

    wrap_op_compute_gas!(address, "ADDRESS", instructions::system::address);
    wrap_op_compute_gas!(balance, "BALANCE", instructions::host::balance);
    wrap_op_compute_gas!(origin, "ORIGIN", instructions::tx_info::origin);
    wrap_op_compute_gas!(caller, "CALLER", instructions::system::caller);
    wrap_op_compute_gas!(callvalue, "CALLVALUE", instructions::system::callvalue);
    wrap_op_compute_gas!(calldataload, "CALLDATALOAD", instructions::system::calldataload);
    wrap_op_compute_gas!(calldatasize, "CALLDATASIZE", instructions::system::calldatasize);
    wrap_op_compute_gas!(calldatacopy, "CALLDATACOPY", instructions::system::calldatacopy);
    wrap_op_compute_gas!(codesize, "CODESIZE", instructions::system::codesize);
    wrap_op_compute_gas!(codecopy, "CODECOPY", instructions::system::codecopy);

    wrap_op_compute_gas!(gasprice, "GASPRICE", instructions::tx_info::gasprice);
    wrap_op_compute_gas!(extcodesize, "EXTCODESIZE", instructions::host::extcodesize);
    wrap_op_compute_gas!(extcodecopy, "EXTCODECOPY", instructions::host::extcodecopy);
    wrap_op_compute_gas!(returndatasize, "RETURNDATASIZE", instructions::system::returndatasize);
    wrap_op_compute_gas!(returndatacopy, "RETURNDATACOPY", instructions::system::returndatacopy);
    wrap_op_compute_gas!(extcodehash, "EXTCODEHASH", instructions::host::extcodehash);
    wrap_op_compute_gas!(blockhash, "BLOCKHASH", instructions::host::blockhash);
    wrap_op_compute_gas!(coinbase, "COINBASE", instructions::block_info::coinbase);
    wrap_op_compute_gas!(timestamp, "TIMESTAMP", instructions::block_info::timestamp);
    wrap_op_compute_gas!(number, "NUMBER", instructions::block_info::block_number);
    wrap_op_compute_gas!(difficulty, "DIFFICULTY", instructions::block_info::difficulty);
    wrap_op_compute_gas!(gaslimit, "GASLIMIT", instructions::block_info::gaslimit);
    wrap_op_compute_gas!(chainid, "CHAINID", instructions::block_info::chainid);
    wrap_op_compute_gas!(selfbalance, "SELFBALANCE", instructions::host::selfbalance);
    wrap_op_compute_gas!(basefee, "BASEFEE", instructions::block_info::basefee);
    wrap_op_compute_gas!(blobhash, "BLOBHASH", instructions::tx_info::blob_hash);
    wrap_op_compute_gas!(blobbasefee, "BLOBBASEFEE", instructions::block_info::blob_basefee);

    wrap_op_compute_gas!(pop, "POP", instructions::stack::pop);
    wrap_op_compute_gas!(mload, "MLOAD", instructions::memory::mload);
    wrap_op_compute_gas!(mstore, "MSTORE", instructions::memory::mstore);
    wrap_op_compute_gas!(mstore8, "MSTORE8", instructions::memory::mstore8);
    wrap_op_compute_gas!(sload, "SLOAD", instructions::host::sload);
    wrap_op_compute_gas!(sstore, "SSTORE", instructions::host::sstore);
    wrap_op_compute_gas!(jump, "JUMP", instructions::control::jump);
    wrap_op_compute_gas!(jumpi, "JUMPI", instructions::control::jumpi);
    wrap_op_compute_gas!(pc, "PC", instructions::control::pc);
    wrap_op_compute_gas!(msize, "MSIZE", instructions::memory::msize);
    wrap_op_compute_gas!(gas, "GAS", instructions::system::gas);
    wrap_op_compute_gas!(jumpdest, "JUMPDEST", instructions::control::jumpdest);
    wrap_op_compute_gas!(tload, "TLOAD", instructions::host::tload);
    wrap_op_compute_gas!(tstore, "TSTORE", instructions::host::tstore);
    wrap_op_compute_gas!(mcopy, "MCOPY", instructions::memory::mcopy);

    wrap_op_compute_gas!(push0, "PUSH0", instructions::stack::push0);
    wrap_op_compute_gas!(push1, "PUSH1", instructions::stack::push::<1, _, _>);
    wrap_op_compute_gas!(push2, "PUSH2", instructions::stack::push::<2, _, _>);
    wrap_op_compute_gas!(push3, "PUSH3", instructions::stack::push::<3, _, _>);
    wrap_op_compute_gas!(push4, "PUSH4", instructions::stack::push::<4, _, _>);
    wrap_op_compute_gas!(push5, "PUSH5", instructions::stack::push::<5, _, _>);
    wrap_op_compute_gas!(push6, "PUSH6", instructions::stack::push::<6, _, _>);
    wrap_op_compute_gas!(push7, "PUSH7", instructions::stack::push::<7, _, _>);
    wrap_op_compute_gas!(push8, "PUSH8", instructions::stack::push::<8, _, _>);
    wrap_op_compute_gas!(push9, "PUSH9", instructions::stack::push::<9, _, _>);
    wrap_op_compute_gas!(push10, "PUSH10", instructions::stack::push::<10, _, _>);
    wrap_op_compute_gas!(push11, "PUSH11", instructions::stack::push::<11, _, _>);
    wrap_op_compute_gas!(push12, "PUSH12", instructions::stack::push::<12, _, _>);
    wrap_op_compute_gas!(push13, "PUSH13", instructions::stack::push::<13, _, _>);
    wrap_op_compute_gas!(push14, "PUSH14", instructions::stack::push::<14, _, _>);
    wrap_op_compute_gas!(push15, "PUSH15", instructions::stack::push::<15, _, _>);
    wrap_op_compute_gas!(push16, "PUSH16", instructions::stack::push::<16, _, _>);
    wrap_op_compute_gas!(push17, "PUSH17", instructions::stack::push::<17, _, _>);
    wrap_op_compute_gas!(push18, "PUSH18", instructions::stack::push::<18, _, _>);
    wrap_op_compute_gas!(push19, "PUSH19", instructions::stack::push::<19, _, _>);
    wrap_op_compute_gas!(push20, "PUSH20", instructions::stack::push::<20, _, _>);
    wrap_op_compute_gas!(push21, "PUSH21", instructions::stack::push::<21, _, _>);
    wrap_op_compute_gas!(push22, "PUSH22", instructions::stack::push::<22, _, _>);
    wrap_op_compute_gas!(push23, "PUSH23", instructions::stack::push::<23, _, _>);
    wrap_op_compute_gas!(push24, "PUSH24", instructions::stack::push::<24, _, _>);
    wrap_op_compute_gas!(push25, "PUSH25", instructions::stack::push::<25, _, _>);
    wrap_op_compute_gas!(push26, "PUSH26", instructions::stack::push::<26, _, _>);
    wrap_op_compute_gas!(push27, "PUSH27", instructions::stack::push::<27, _, _>);
    wrap_op_compute_gas!(push28, "PUSH28", instructions::stack::push::<28, _, _>);
    wrap_op_compute_gas!(push29, "PUSH29", instructions::stack::push::<29, _, _>);
    wrap_op_compute_gas!(push30, "PUSH30", instructions::stack::push::<30, _, _>);
    wrap_op_compute_gas!(push31, "PUSH31", instructions::stack::push::<31, _, _>);
    wrap_op_compute_gas!(push32, "PUSH32", instructions::stack::push::<32, _, _>);

    wrap_op_compute_gas!(dup1, "DUP1", instructions::stack::dup::<1, _, _>);
    wrap_op_compute_gas!(dup2, "DUP2", instructions::stack::dup::<2, _, _>);
    wrap_op_compute_gas!(dup3, "DUP3", instructions::stack::dup::<3, _, _>);
    wrap_op_compute_gas!(dup4, "DUP4", instructions::stack::dup::<4, _, _>);
    wrap_op_compute_gas!(dup5, "DUP5", instructions::stack::dup::<5, _, _>);
    wrap_op_compute_gas!(dup6, "DUP6", instructions::stack::dup::<6, _, _>);
    wrap_op_compute_gas!(dup7, "DUP7", instructions::stack::dup::<7, _, _>);
    wrap_op_compute_gas!(dup8, "DUP8", instructions::stack::dup::<8, _, _>);
    wrap_op_compute_gas!(dup9, "DUP9", instructions::stack::dup::<9, _, _>);
    wrap_op_compute_gas!(dup10, "DUP10", instructions::stack::dup::<10, _, _>);
    wrap_op_compute_gas!(dup11, "DUP11", instructions::stack::dup::<11, _, _>);
    wrap_op_compute_gas!(dup12, "DUP12", instructions::stack::dup::<12, _, _>);
    wrap_op_compute_gas!(dup13, "DUP13", instructions::stack::dup::<13, _, _>);
    wrap_op_compute_gas!(dup14, "DUP14", instructions::stack::dup::<14, _, _>);
    wrap_op_compute_gas!(dup15, "DUP15", instructions::stack::dup::<15, _, _>);
    wrap_op_compute_gas!(dup16, "DUP16", instructions::stack::dup::<16, _, _>);

    wrap_op_compute_gas!(swap1, "SWAP1", instructions::stack::swap::<1, _, _>);
    wrap_op_compute_gas!(swap2, "SWAP2", instructions::stack::swap::<2, _, _>);
    wrap_op_compute_gas!(swap3, "SWAP3", instructions::stack::swap::<3, _, _>);
    wrap_op_compute_gas!(swap4, "SWAP4", instructions::stack::swap::<4, _, _>);
    wrap_op_compute_gas!(swap5, "SWAP5", instructions::stack::swap::<5, _, _>);
    wrap_op_compute_gas!(swap6, "SWAP6", instructions::stack::swap::<6, _, _>);
    wrap_op_compute_gas!(swap7, "SWAP7", instructions::stack::swap::<7, _, _>);
    wrap_op_compute_gas!(swap8, "SWAP8", instructions::stack::swap::<8, _, _>);
    wrap_op_compute_gas!(swap9, "SWAP9", instructions::stack::swap::<9, _, _>);
    wrap_op_compute_gas!(swap10, "SWAP10", instructions::stack::swap::<10, _, _>);
    wrap_op_compute_gas!(swap11, "SWAP11", instructions::stack::swap::<11, _, _>);
    wrap_op_compute_gas!(swap12, "SWAP12", instructions::stack::swap::<12, _, _>);
    wrap_op_compute_gas!(swap13, "SWAP13", instructions::stack::swap::<13, _, _>);
    wrap_op_compute_gas!(swap14, "SWAP14", instructions::stack::swap::<14, _, _>);
    wrap_op_compute_gas!(swap15, "SWAP15", instructions::stack::swap::<15, _, _>);
    wrap_op_compute_gas!(swap16, "SWAP16", instructions::stack::swap::<16, _, _>);

    wrap_op_compute_gas!(log0, "LOG0", instructions::host::log::<0, _>);
    wrap_op_compute_gas!(log1, "LOG1", instructions::host::log::<1, _>);
    wrap_op_compute_gas!(log2, "LOG2", instructions::host::log::<2, _>);
    wrap_op_compute_gas!(log3, "LOG3", instructions::host::log::<3, _>);
    wrap_op_compute_gas!(log4, "LOG4", instructions::host::log::<4, _>);

    wrap_op_compute_gas!(create, "CREATE", instructions::contract::create::<_, false, _>);
    wrap_op_compute_gas!(call, "CALL", instructions::contract::call);
    wrap_op_compute_gas!(call_code, "CALLCODE", instructions::contract::call_code);
    wrap_op_compute_gas!(ret, "RETURN", instructions::control::ret);
    wrap_op_compute_gas!(delegate_call, "DELEGATECALL", instructions::contract::delegate_call);
    wrap_op_compute_gas!(create2, "CREATE2", instructions::contract::create::<_, true, _>);
    wrap_op_compute_gas!(static_call, "STATICCALL", instructions::contract::static_call);

    wrap_op_compute_gas!(revert, "REVERT", instructions::control::revert);
    wrap_op_compute_gas!(invalid, "INVALID", instructions::control::invalid);
    wrap_op_compute_gas!(selfdestruct, "SELFDESTRUCT", instructions::host::selfdestruct);
}

/// Trait to inspect the stack elements.
pub trait StackInspectTr {
    /// Inspect the N-th element of the stack. The top of the stack is the 0-th element.
    /// If the stack is too short, return None.
    fn inspect<const N: usize>(&self) -> Option<U256>;
}

impl StackInspectTr for Stack {
    fn inspect<const N: usize>(&self) -> Option<U256> {
        if N >= self.len() {
            return None;
        }
        let index = self.len() - 1 - N;
        // SAFETY: the index must be within the bounds of the stack
        Some(unsafe { *self.data().get_unchecked(index) })
    }
}