monad-revm 0.5.0

Monad-specific REVM implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
use crate::MonadHardfork;
use revm::{
    context_interface::cfg::{GasId, GasParams},
    handler::instructions::{EthInstructions, InstructionProvider},
    interpreter::{
        instructions::{gas_table_spec, instruction_table, Instruction},
        interpreter::EthInterpreter,
        Host,
    },
    primitives::hardfork::SpecId,
};

/// Type alias for Monad instructions.
pub type MonadInstructions<CTX> = EthInstructions<EthInterpreter, CTX>;

/// Instruction provider that follows Monad hardfork changes between frames.
#[auto_impl::auto_impl(&mut, Box)]
pub trait MonadInstructionProvider: InstructionProvider {
    /// Selects the instructions for a Monad hardfork.
    fn set_spec(&mut self, spec: MonadHardfork);

    /// Selects the instructions for a frame's underlying Ethereum hardfork.
    fn set_frame_spec(&mut self, spec: SpecId);
}

/// Maps a frame's Ethereum runtime spec to its Monad instruction and precompile behavior.
///
/// MonadNine and MonadNext currently share Osaka behavior, so an Osaka frame restores the
/// MonadNine provider configuration.
pub(crate) const fn monad_frame_spec(spec: SpecId) -> MonadHardfork {
    if spec.is_enabled_in(SpecId::OSAKA) {
        MonadHardfork::MonadNine
    } else {
        MonadHardfork::MonadEight
    }
}

/// Monad-specific gas parameters for a given hardfork.
/// Override Ethereum defaults with Monad's gas costs.
///
/// Monad increases cold access costs to account for the relatively higher cost
/// of state reads from disk. See: <https://docs.monad.xyz/developer-essentials/opcode-pricing#cold-access-cost>
///
/// | Access Type | Ethereum | Monad |
/// |-------------|----------|-------|
/// | Account     | 2600     | 10100 |
/// | Storage     | 2100     | 8100  |
///
/// Warm access costs (100 gas) remain the same as Ethereum.
pub fn monad_gas_params(spec: MonadHardfork) -> GasParams {
    let eth_spec = spec.into_eth_spec();
    let mut params = GasParams::new_spec(eth_spec);

    if MonadHardfork::MonadEight.is_enabled_in(spec) {
        params.override_gas([
            // SSTORE uses full cold storage cost
            (GasId::cold_storage_cost(), COLD_SLOAD_COST),
            // SLOAD uses additional cost (cold - warm)
            (GasId::cold_storage_additional_cost(), COLD_SLOAD_COST - WARM_STORAGE_READ_COST),
            // Account access opcodes (BALANCE, EXTCODESIZE, EXTCODECOPY, EXTCODEHASH,
            // CALL, CALLCODE, DELEGATECALL, STATICCALL, SELFDESTRUCT) use additional cost
            (
                GasId::cold_account_additional_cost(),
                COLD_ACCOUNT_ACCESS_COST - WARM_STORAGE_READ_COST,
            ),
        ]);
    }

    params
}

/// Create Monad instructions table with custom gas costs.
///
/// For all supported Monad specs, CREATE/CREATE2 use Monad-local handlers so
/// delegated accounts cannot create contracts. MonadNine+ additionally replaces
/// memory-expanding opcodes with linear-cost MIP-3 handlers (`words / 2`).
pub fn monad_instructions<CTX: Host>(spec: MonadHardfork) -> MonadInstructions<CTX> {
    let eth_spec = spec.into_eth_spec();
    let mut instructions =
        EthInstructions::new(instruction_table(), gas_table_spec(eth_spec), eth_spec);

    // All supported Monad specs forbid CREATE/CREATE2 while executing on behalf of
    // an EIP-7702 delegated account.
    use crate::memory::opcodes;
    use revm::bytecode::opcode::*;
    instructions.insert_instruction(CREATE, Instruction::new(opcodes::create::<_, false, _>), 0);
    instructions.insert_instruction(CREATE2, Instruction::new(opcodes::create::<_, true, _>), 0);
    instructions.insert_instruction(
        CALL,
        Instruction::new(opcodes::call),
        WARM_STORAGE_READ_COST as u16,
    );
    instructions.insert_instruction(
        CALLCODE,
        Instruction::new(opcodes::call_code),
        WARM_STORAGE_READ_COST as u16,
    );
    instructions.insert_instruction(
        DELEGATECALL,
        Instruction::new(opcodes::delegate_call),
        WARM_STORAGE_READ_COST as u16,
    );
    instructions.insert_instruction(
        STATICCALL,
        Instruction::new(opcodes::static_call),
        WARM_STORAGE_READ_COST as u16,
    );

    // MIP-3: Replace memory-expanding opcodes with linear-cost variants.
    if MonadHardfork::MonadNine.is_enabled_in(spec) {
        use revm::interpreter::instructions::gas;

        // Memory opcodes
        instructions.insert_instruction(MLOAD, Instruction::new(opcodes::mload), 3);
        instructions.insert_instruction(MSTORE, Instruction::new(opcodes::mstore), 3);
        instructions.insert_instruction(MSTORE8, Instruction::new(opcodes::mstore8), 3);
        instructions.insert_instruction(MCOPY, Instruction::new(opcodes::mcopy), 3);

        // Hash
        instructions.insert_instruction(
            KECCAK256,
            Instruction::new(opcodes::keccak256),
            gas::KECCAK256 as u16,
        );

        // Copy opcodes
        instructions.insert_instruction(CALLDATACOPY, Instruction::new(opcodes::calldatacopy), 3);
        instructions.insert_instruction(CODECOPY, Instruction::new(opcodes::codecopy), 3);
        instructions.insert_instruction(
            RETURNDATACOPY,
            Instruction::new(opcodes::returndatacopy),
            3,
        );
        instructions.insert_instruction(
            EXTCODECOPY,
            Instruction::new(opcodes::extcodecopy),
            gas::WARM_STORAGE_READ_COST as u16,
        );

        // Log opcodes
        instructions.insert_instruction(
            LOG0,
            Instruction::new(opcodes::log::<0, _>),
            gas::LOG as u16,
        );
        instructions.insert_instruction(
            LOG1,
            Instruction::new(opcodes::log::<1, _>),
            gas::LOG as u16,
        );
        instructions.insert_instruction(
            LOG2,
            Instruction::new(opcodes::log::<2, _>),
            gas::LOG as u16,
        );
        instructions.insert_instruction(
            LOG3,
            Instruction::new(opcodes::log::<3, _>),
            gas::LOG as u16,
        );
        instructions.insert_instruction(
            LOG4,
            Instruction::new(opcodes::log::<4, _>),
            gas::LOG as u16,
        );

        // Return opcodes
        instructions.insert_instruction(RETURN, Instruction::new(opcodes::ret), 0);
        instructions.insert_instruction(REVERT, Instruction::new(opcodes::revert), 0);
    }

    instructions
}

impl<CTX: Host> MonadInstructionProvider for MonadInstructions<CTX> {
    fn set_spec(&mut self, spec: MonadHardfork) {
        if self.spec != spec.into_eth_spec() {
            *self = monad_instructions(spec);
        }
    }

    fn set_frame_spec(&mut self, spec: SpecId) {
        self.set_spec(monad_frame_spec(spec));
    }
}

/// Monad cold storage access cost (SLOAD, SSTORE).
/// Ethereum: 2100, Monad: 8100
pub const COLD_SLOAD_COST: u64 = 8100;

/// Monad cold account access cost (BALANCE, EXTCODE*, CALL*, SELFDESTRUCT).
/// Ethereum: 2600, Monad: 10100
pub const COLD_ACCOUNT_ACCESS_COST: u64 = 10100;

/// Warm storage read cost - same as Ethereum.
pub const WARM_STORAGE_READ_COST: u64 = 100;

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(feature = "memory_limit")]
    use crate::cfg::MONAD_MEMORY_LIMIT;
    use crate::{
        api::{
            builder::MonadBuilder,
            default_ctx::{monad_context_with_db, MonadContext},
        },
        precompiles::MonadPrecompiles,
        reserve_balance::{
            abi::RESERVE_BALANCE_ADDRESS, interface::IReserveBalance::dippedIntoReserveCall,
        },
        staking::{interface::IMonadStaking::getEpochCall, storage::STAKING_ADDRESS},
        MonadCfgEnv,
    };
    use alloc::{string::String, vec, vec::Vec};
    use alloy_sol_types::SolCall;
    #[cfg(feature = "memory_limit")]
    use revm::context_interface::result::OutOfGasError;
    use revm::{
        bytecode::opcode,
        context::TxEnv,
        context_interface::result::{ExecutionResult, HaltReason},
        database::InMemoryDB,
        handler::{EvmTr, PrecompileProvider},
        inspector::InspectEvm,
        interpreter::{CallInputs, InterpreterResult},
        primitives::{hardfork::SpecId, Address, AddressSet, Bytes, TxKind, U256},
        state::{AccountInfo, Bytecode},
        ExecuteEvm, Inspector,
    };
    use std::{cell::RefCell, rc::Rc};

    const DUPN_OPCODE: u8 = 0xE6;
    const SWAPN_OPCODE: u8 = 0xE7;
    const EXCHANGE_OPCODE: u8 = 0xE8;

    #[test]
    fn test_monad_gas_params_cold_storage_cost() {
        let params = monad_gas_params(MonadHardfork::MonadEight);
        assert_eq!(params.get(GasId::cold_storage_cost()), COLD_SLOAD_COST);
    }

    #[test]
    fn test_monad_gas_params_cold_storage_additional_cost() {
        let params = monad_gas_params(MonadHardfork::MonadEight);
        assert_eq!(
            params.get(GasId::cold_storage_additional_cost()),
            COLD_SLOAD_COST - WARM_STORAGE_READ_COST
        );
    }

    #[test]
    fn test_monad_gas_params_cold_account_additional_cost() {
        let params = monad_gas_params(MonadHardfork::MonadEight);
        assert_eq!(
            params.get(GasId::cold_account_additional_cost()),
            COLD_ACCOUNT_ACCESS_COST - WARM_STORAGE_READ_COST
        );
    }

    #[test]
    fn test_monad_gas_params_warm_storage_unchanged() {
        let params = monad_gas_params(MonadHardfork::MonadEight);
        assert_eq!(params.get(GasId::warm_storage_read_cost()), WARM_STORAGE_READ_COST);
    }

    #[test]
    fn test_monad_vs_ethereum_cold_costs() {
        let monad = monad_gas_params(MonadHardfork::MonadEight);
        let eth = GasParams::new_spec(SpecId::PRAGUE);

        // Monad cold storage: 8100 vs Ethereum: 2100
        assert_eq!(monad.get(GasId::cold_storage_cost()), 8100);
        assert_eq!(eth.get(GasId::cold_storage_cost()), 2100);

        // Monad cold account additional: 10000 vs Ethereum: 2500
        assert_eq!(monad.get(GasId::cold_account_additional_cost()), 10000);
        assert_eq!(eth.get(GasId::cold_account_additional_cost()), 2500);
    }

    fn run_contract(spec: MonadHardfork, code: Vec<u8>) -> ExecutionResult<HaltReason> {
        let caller = Address::from([0x11; 20]);
        let contract = Address::from([0x22; 20]);

        let mut db = InMemoryDB::default();
        db.insert_account_info(
            caller,
            AccountInfo { balance: U256::from(1_000_000u64), ..Default::default() },
        );
        db.insert_account_info(
            contract,
            AccountInfo::default().with_code(Bytecode::new_raw(Bytes::from(code))),
        );

        let ctx = monad_context_with_db(db).with_cfg(MonadCfgEnv::new_with_spec(spec));
        let mut evm = ctx.build_monad();
        evm.ctx().block.basefee = 0;

        let tx = TxEnv::builder()
            .caller(caller)
            .kind(TxKind::Call(contract))
            .gas_limit(100_000)
            .gas_price(0)
            .build_fill();

        evm.transact(tx).expect("contract call should execute").result
    }

    fn run_delegated_contract(
        spec: MonadHardfork,
        target_code: Bytecode,
        delegated_address: Address,
        delegated_code: Vec<u8>,
        extra_accounts: &[(Address, Bytecode)],
    ) -> ExecutionResult<HaltReason> {
        let caller = Address::from([0x11; 20]);
        let target = Address::from([0x22; 20]);

        let mut db = InMemoryDB::default();
        db.insert_account_info(
            caller,
            AccountInfo { balance: U256::from(1_000_000u64), ..Default::default() },
        );
        db.insert_account_info(target, AccountInfo::default().with_code(target_code));
        db.insert_account_info(
            delegated_address,
            AccountInfo::default().with_code(Bytecode::new_raw(Bytes::from(delegated_code))),
        );
        for (address, code) in extra_accounts {
            db.insert_account_info(*address, AccountInfo::default().with_code(code.clone()));
        }

        let ctx = monad_context_with_db(db).with_cfg(MonadCfgEnv::new_with_spec(spec));
        let mut evm = ctx.build_monad();
        evm.ctx().block.basefee = 0;

        let tx = TxEnv::builder()
            .caller(caller)
            .kind(TxKind::Call(target))
            .gas_limit(1_000_000)
            .gas_price(0)
            .build_fill();

        evm.transact(tx).expect("delegated contract call should execute").result
    }

    fn run_contract_with_input_and_accounts(
        spec: MonadHardfork,
        target_code: Bytecode,
        input: Bytes,
        extra_accounts: &[(Address, Bytecode)],
    ) -> ExecutionResult<HaltReason> {
        let caller = Address::from([0x11; 20]);
        let target = Address::from([0x22; 20]);

        let mut db = InMemoryDB::default();
        db.insert_account_info(
            caller,
            AccountInfo { balance: U256::from(1_000_000u64), ..Default::default() },
        );
        db.insert_account_info(target, AccountInfo::default().with_code(target_code));
        for (address, code) in extra_accounts {
            db.insert_account_info(*address, AccountInfo::default().with_code(code.clone()));
        }

        let ctx = monad_context_with_db(db).with_cfg(MonadCfgEnv::new_with_spec(spec));
        let mut evm = ctx.build_monad();
        evm.ctx().block.basefee = 0;

        let tx = TxEnv::builder()
            .caller(caller)
            .kind(TxKind::Call(target))
            .gas_limit(1_000_000)
            .gas_price(0)
            .data(input)
            .build_fill();

        evm.transact(tx).expect("contract call should execute").result
    }

    fn call_returns_success_flag_contract(target: Address, selector: [u8; 4]) -> Vec<u8> {
        let mut code = vec![opcode::PUSH4];
        code.extend_from_slice(&selector);
        code.extend_from_slice(&[
            opcode::PUSH1,
            0x1c,
            opcode::MSTORE,
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::PUSH1,
            0x04,
            opcode::PUSH1,
            0x1c,
            opcode::PUSH0,
            opcode::PUSH20,
        ]);
        code.extend_from_slice(target.as_slice());
        code.extend_from_slice(&[
            opcode::GAS,
            opcode::CALL,
            opcode::PUSH0,
            opcode::MSTORE,
            opcode::PUSH1,
            0x20,
            opcode::PUSH0,
            opcode::RETURN,
        ]);
        code
    }

    fn push2(code: &mut Vec<u8>, value: u16) {
        code.push(opcode::PUSH2);
        code.extend_from_slice(&value.to_be_bytes());
    }

    fn memory_expanding_call_contract(
        opcode: u8,
        target: Address,
        input_len: u16,
        output_len: u16,
    ) -> Vec<u8> {
        let mut code = Vec::new();
        push2(&mut code, output_len);
        push2(&mut code, 0x2000);
        push2(&mut code, input_len);
        push2(&mut code, 0x1000);

        match opcode {
            opcode::CALL | opcode::CALLCODE => {
                code.push(opcode::PUSH0); // value
                code.push(opcode::PUSH20);
                code.extend_from_slice(target.as_slice());
                code.push(opcode::GAS);
                code.push(opcode);
            }
            opcode::DELEGATECALL | opcode::STATICCALL => {
                code.push(opcode::PUSH20);
                code.extend_from_slice(target.as_slice());
                code.push(opcode::GAS);
                code.push(opcode);
            }
            _ => unreachable!("only CALL-like opcodes are supported"),
        }

        code.push(opcode::STOP);
        code
    }

    fn run_memory_expanding_call(
        spec: MonadHardfork,
        opcode: u8,
        input_len: u16,
        output_len: u16,
    ) -> u64 {
        let callee = Address::from([0x44; 20]);
        let code = memory_expanding_call_contract(opcode, callee, input_len, output_len);
        let result = run_contract_with_input_and_accounts(
            spec,
            Bytecode::new_raw(Bytes::from(code)),
            Bytes::new(),
            &[(callee, Bytecode::new_raw(Bytes::new()))],
        );

        assert!(
            matches!(result, ExecutionResult::Success { .. }),
            "memory-expanding CALL-like contract should succeed on {spec:?}"
        );
        result.tx_gas_used()
    }

    const fn standard_memory_cost(words: u64) -> u64 {
        3 * words + words * words / 512
    }

    #[derive(Clone, Copy, Debug)]
    struct SwitchSpecInspector {
        target: Address,
        spec: MonadHardfork,
    }

    impl Inspector<MonadContext<InMemoryDB>> for SwitchSpecInspector {
        fn call(
            &mut self,
            context: &mut MonadContext<InMemoryDB>,
            inputs: &mut CallInputs,
        ) -> Option<revm::interpreter::CallOutcome> {
            if inputs.target_address == self.target {
                let mut cfg = context.cfg.clone().into_inner();
                cfg.spec = self.spec;
                context.cfg = MonadCfgEnv::from(cfg);
            }
            None
        }
    }

    #[derive(Clone, Debug)]
    struct TrackingPrecompiles {
        inner: MonadPrecompiles,
        selected_specs: Rc<RefCell<Vec<MonadHardfork>>>,
    }

    impl PrecompileProvider<MonadContext<InMemoryDB>> for TrackingPrecompiles {
        type Output = InterpreterResult;

        fn set_spec(&mut self, spec: MonadHardfork) -> bool {
            self.selected_specs.borrow_mut().push(spec);
            PrecompileProvider::<MonadContext<InMemoryDB>>::set_spec(&mut self.inner, spec)
        }

        fn run(
            &mut self,
            context: &mut MonadContext<InMemoryDB>,
            inputs: &CallInputs,
        ) -> Result<Option<Self::Output>, String> {
            PrecompileProvider::<MonadContext<InMemoryDB>>::run(&mut self.inner, context, inputs)
        }

        fn warm_addresses(&self) -> &AddressSet {
            PrecompileProvider::<MonadContext<InMemoryDB>>::warm_addresses(&self.inner)
        }

        fn contains(&self, address: &Address) -> bool {
            PrecompileProvider::<MonadContext<InMemoryDB>>::contains(&self.inner, address)
        }
    }

    #[derive(Clone, Debug)]
    struct FailingPrecompiles {
        inner: TrackingPrecompiles,
        fail_address: Address,
        fail_next: bool,
    }

    impl PrecompileProvider<MonadContext<InMemoryDB>> for FailingPrecompiles {
        type Output = InterpreterResult;

        fn set_spec(&mut self, spec: MonadHardfork) -> bool {
            PrecompileProvider::<MonadContext<InMemoryDB>>::set_spec(&mut self.inner, spec)
        }

        fn run(
            &mut self,
            context: &mut MonadContext<InMemoryDB>,
            inputs: &CallInputs,
        ) -> Result<Option<Self::Output>, String> {
            if self.fail_next && inputs.bytecode_address == self.fail_address {
                self.fail_next = false;
                return Err("intentional precompile failure".into());
            }
            PrecompileProvider::<MonadContext<InMemoryDB>>::run(&mut self.inner, context, inputs)
        }

        fn warm_addresses(&self) -> &AddressSet {
            PrecompileProvider::<MonadContext<InMemoryDB>>::warm_addresses(&self.inner)
        }

        fn contains(&self, address: &Address) -> bool {
            PrecompileProvider::<MonadContext<InMemoryDB>>::contains(&self.inner, address)
        }
    }

    fn store_at(offset: u32) -> Vec<u8> {
        let mut code = vec![opcode::PUSH0, opcode::PUSH3];
        code.extend_from_slice(&offset.to_be_bytes()[1..]);
        code.extend_from_slice(&[opcode::MSTORE, opcode::STOP]);
        code
    }

    fn call_then_store_at(target: Address, offset: u16) -> Vec<u8> {
        let mut code = vec![
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::PUSH20,
        ];
        code.extend_from_slice(target.as_slice());
        code.extend_from_slice(&[
            opcode::GAS,
            opcode::CALL,
            opcode::POP,
            opcode::PUSH0,
            opcode::PUSH2,
        ]);
        code.extend_from_slice(&offset.to_be_bytes());
        code.extend_from_slice(&[opcode::MSTORE, opcode::STOP]);
        code
    }

    #[cfg(feature = "memory_limit")]
    fn call_and_return_success(target: Address) -> Vec<u8> {
        let mut code = vec![
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::PUSH20,
        ];
        code.extend_from_slice(target.as_slice());
        code.extend_from_slice(&[
            opcode::GAS,
            opcode::CALL,
            opcode::PUSH0,
            opcode::MSTORE,
            opcode::PUSH1,
            0x20,
            opcode::PUSH0,
            opcode::RETURN,
        ]);
        code
    }

    fn run_frame_spec_transition(
        parent_spec: MonadHardfork,
        child_spec: MonadHardfork,
        child_offset: u32,
        parent_offset: u16,
    ) -> u64 {
        let caller = Address::from([0x11; 20]);
        let parent = Address::from([0x22; 20]);
        let child = Address::from([0x33; 20]);

        let mut db = InMemoryDB::default();
        db.insert_account_info(
            caller,
            AccountInfo { balance: U256::from(1_000_000u64), ..Default::default() },
        );
        db.insert_account_info(
            parent,
            AccountInfo::default().with_code(Bytecode::new_raw(Bytes::from(call_then_store_at(
                child,
                parent_offset,
            )))),
        );
        db.insert_account_info(
            child,
            AccountInfo::default()
                .with_code(Bytecode::new_raw(Bytes::from(store_at(child_offset)))),
        );

        let ctx = monad_context_with_db(db).with_cfg(MonadCfgEnv::new_with_spec(parent_spec));
        let inspector = SwitchSpecInspector { target: child, spec: child_spec };
        let selected_specs = Rc::new(RefCell::new(Vec::new()));
        let precompiles = TrackingPrecompiles {
            inner: MonadPrecompiles::new_with_spec(parent_spec),
            selected_specs: Rc::clone(&selected_specs),
        };
        let mut evm = ctx.build_monad_with_inspector(inspector).with_precompiles(precompiles);
        evm.ctx().block.basefee = 0;

        let tx = TxEnv::builder()
            .caller(caller)
            .kind(TxKind::Call(parent))
            .gas_limit(1_000_000)
            .gas_price(0)
            .build_fill();
        let result = evm.inspect_one_tx(tx).expect("transitioning contract call should execute");
        assert!(
            matches!(result, ExecutionResult::Success { .. }),
            "transitioning contract call should succeed: {parent_spec:?} -> {child_spec:?}"
        );
        assert!(
            selected_specs
                .borrow()
                .windows(3)
                .any(|specs| specs == [parent_spec, child_spec, parent_spec]),
            "precompile provider should follow and restore frame specs"
        );
        result.tx_gas_used()
    }

    fn run_immediate_precompile_transition(
        parent_spec: MonadHardfork,
        child_spec: MonadHardfork,
        parent_offset: u16,
    ) -> u64 {
        let caller = Address::from([0x11; 20]);
        let parent = Address::from([0x22; 20]);
        let precompile = revm::precompile::u64_to_address(4);

        let mut db = InMemoryDB::default();
        db.insert_account_info(
            caller,
            AccountInfo { balance: U256::from(1_000_000u64), ..Default::default() },
        );
        db.insert_account_info(
            parent,
            AccountInfo::default().with_code(Bytecode::new_raw(Bytes::from(call_then_store_at(
                precompile,
                parent_offset,
            )))),
        );

        let ctx = monad_context_with_db(db).with_cfg(MonadCfgEnv::new_with_spec(parent_spec));
        let inspector = SwitchSpecInspector { target: precompile, spec: child_spec };
        let selected_specs = Rc::new(RefCell::new(Vec::new()));
        let precompiles = TrackingPrecompiles {
            inner: MonadPrecompiles::new_with_spec(parent_spec),
            selected_specs: Rc::clone(&selected_specs),
        };
        let mut evm = ctx.build_monad_with_inspector(inspector).with_precompiles(precompiles);
        evm.ctx().block.basefee = 0;

        let tx = TxEnv::builder()
            .caller(caller)
            .kind(TxKind::Call(parent))
            .gas_limit(1_000_000)
            .gas_price(0)
            .build_fill();
        let result = evm.inspect_one_tx(tx).expect("nested precompile call should execute");
        assert!(
            matches!(result, ExecutionResult::Success { .. }),
            "nested precompile call should succeed: {parent_spec:?} -> {child_spec:?}"
        );
        assert!(
            selected_specs
                .borrow()
                .windows(3)
                .any(|specs| specs == [parent_spec, child_spec, parent_spec]),
            "precompile provider should restore the parent after an immediate result"
        );
        result.tx_gas_used()
    }

    #[cfg(feature = "memory_limit")]
    fn run_memory_limit_contract(offset: u32) -> ExecutionResult<HaltReason> {
        let caller = Address::from([0x11; 20]);
        let contract = Address::from([0x22; 20]);

        let mut db = InMemoryDB::default();
        db.insert_account_info(
            caller,
            AccountInfo { balance: U256::from(1_000_000u64), ..Default::default() },
        );
        db.insert_account_info(
            contract,
            AccountInfo::default().with_code(Bytecode::new_raw(Bytes::from(store_at(offset)))),
        );

        let mut cfg = MonadCfgEnv::new_with_spec(MonadHardfork::MonadNine);
        cfg.0.memory_limit = 128 * 1024 * 1024;
        let ctx = monad_context_with_db(db).with_cfg(cfg);
        let mut evm = ctx.build_monad();
        evm.ctx().block.basefee = 0;

        let tx = TxEnv::builder()
            .caller(caller)
            .kind(TxKind::Call(contract))
            .gas_limit(1_000_000)
            .gas_price(0)
            .build_fill();
        evm.transact(tx).expect("memory limit contract should execute").result
    }

    #[cfg(feature = "memory_limit")]
    fn run_frame_memory_limit_transition(
        parent_spec: MonadHardfork,
        child_spec: MonadHardfork,
        child_offset: u32,
    ) -> bool {
        let caller = Address::from([0x11; 20]);
        let parent = Address::from([0x22; 20]);
        let child = Address::from([0x33; 20]);

        let mut db = InMemoryDB::default();
        db.insert_account_info(
            caller,
            AccountInfo { balance: U256::from(1_000_000u64), ..Default::default() },
        );
        db.insert_account_info(
            parent,
            AccountInfo::default()
                .with_code(Bytecode::new_raw(Bytes::from(call_and_return_success(child)))),
        );
        db.insert_account_info(
            child,
            AccountInfo::default()
                .with_code(Bytecode::new_raw(Bytes::from(store_at(child_offset)))),
        );

        let mut cfg = MonadCfgEnv::new_with_spec(parent_spec);
        cfg.0.memory_limit = 128 * 1024 * 1024;
        cfg.0.tx_gas_limit_cap = Some(u64::MAX);
        let ctx = monad_context_with_db(db).with_cfg(cfg);
        let inspector = SwitchSpecInspector { target: child, spec: child_spec };
        let mut evm = ctx.build_monad_with_inspector(inspector);
        evm.ctx().block.basefee = 0;
        evm.ctx().block.gas_limit = 300_000_000;

        let tx = TxEnv::builder()
            .caller(caller)
            .kind(TxKind::Call(parent))
            .gas_limit(300_000_000)
            .gas_price(0)
            .build_fill();
        let result = evm.inspect_one_tx(tx).expect("transitioning contract call should execute");
        assert!(
            matches!(result, ExecutionResult::Success { .. }),
            "transitioning contract call should succeed: {parent_spec:?} -> {child_spec:?}"
        );
        U256::from_be_slice(
            result.output().expect("parent contract should return the child success flag").as_ref(),
        ) == U256::from(1)
    }

    fn memory_expansion_delta(spec: MonadHardfork) -> u64 {
        let base_words = 1;
        let expanded_words = (0x2000 + 0x20) / 32;
        if MonadHardfork::MonadNine.is_enabled_in(spec) {
            expanded_words / 2 - base_words / 2
        } else {
            standard_memory_cost(expanded_words) - standard_memory_cost(base_words)
        }
    }

    #[test]
    fn test_call_like_memory_expansion_cost_is_spec_dependent() {
        let expanded_words = (0x2000 + 0x20) / 32;
        let standard_cost = standard_memory_cost(expanded_words);
        let mip3_cost = crate::memory::monad_memory_cost(expanded_words as usize);

        for opcode in [opcode::CALL, opcode::CALLCODE, opcode::DELEGATECALL, opcode::STATICCALL] {
            let monad_eight_base =
                run_memory_expanding_call(MonadHardfork::MonadEight, opcode, 0, 0);
            let monad_eight_expanded =
                run_memory_expanding_call(MonadHardfork::MonadEight, opcode, 0x20, 0x20);
            assert_eq!(
                monad_eight_expanded - monad_eight_base,
                standard_cost,
                "MonadEight should use standard revm memory expansion for opcode 0x{opcode:02x}"
            );

            let monad_nine_base = run_memory_expanding_call(MonadHardfork::MonadNine, opcode, 0, 0);
            let monad_nine_expanded =
                run_memory_expanding_call(MonadHardfork::MonadNine, opcode, 0x20, 0x20);
            assert_eq!(
                monad_nine_expanded - monad_nine_base,
                mip3_cost,
                "MonadNine should use MIP-3 memory expansion for opcode 0x{opcode:02x}"
            );
        }
    }

    #[test]
    fn test_instruction_provider_follows_frame_spec_transitions() {
        for (parent_spec, child_spec) in [
            (MonadHardfork::MonadEight, MonadHardfork::MonadNine),
            (MonadHardfork::MonadNine, MonadHardfork::MonadEight),
        ] {
            let base = run_frame_spec_transition(parent_spec, child_spec, 0, 0);
            let child_expanded = run_frame_spec_transition(parent_spec, child_spec, 0x2000, 0);
            assert_eq!(
                child_expanded - base,
                memory_expansion_delta(child_spec),
                "child frame should use {child_spec:?} memory pricing"
            );

            let parent_expanded = run_frame_spec_transition(parent_spec, child_spec, 0, 0x2000);
            assert_eq!(
                parent_expanded - base,
                memory_expansion_delta(parent_spec),
                "parent frame should restore {parent_spec:?} memory pricing"
            );
        }
    }

    #[test]
    fn test_immediate_precompile_restores_parent_frame_spec() {
        for (parent_spec, child_spec) in [
            (MonadHardfork::MonadEight, MonadHardfork::MonadNine),
            (MonadHardfork::MonadNine, MonadHardfork::MonadEight),
        ] {
            let base = run_immediate_precompile_transition(parent_spec, child_spec, 0);
            let parent_expanded =
                run_immediate_precompile_transition(parent_spec, child_spec, 0x2000);
            assert_eq!(
                parent_expanded - base,
                memory_expansion_delta(parent_spec),
                "parent frame should restore {parent_spec:?} pricing after an immediate precompile"
            );
        }
    }

    #[test]
    fn test_frame_spec_is_reset_after_precompile_error() {
        let parent_spec = MonadHardfork::MonadEight;
        let child_spec = MonadHardfork::MonadNine;
        let caller = Address::from([0x11; 20]);
        let first = Address::from([0x22; 20]);
        let second = Address::from([0x33; 20]);
        let precompile = revm::precompile::u64_to_address(4);

        let mut db = InMemoryDB::default();
        db.insert_account_info(
            caller,
            AccountInfo { balance: U256::from(1_000_000u64), ..Default::default() },
        );
        db.insert_account_info(
            first,
            AccountInfo::default()
                .with_code(Bytecode::new_raw(Bytes::from(call_then_store_at(precompile, 0)))),
        );
        db.insert_account_info(
            second,
            AccountInfo::default().with_code(Bytecode::new_raw(Bytes::from(store_at(0)))),
        );

        let ctx = monad_context_with_db(db).with_cfg(MonadCfgEnv::new_with_spec(parent_spec));
        let inspector = SwitchSpecInspector { target: precompile, spec: child_spec };
        let selected_specs = Rc::new(RefCell::new(Vec::new()));
        let precompiles = FailingPrecompiles {
            inner: TrackingPrecompiles {
                inner: MonadPrecompiles::new_with_spec(parent_spec),
                selected_specs: Rc::clone(&selected_specs),
            },
            fail_address: precompile,
            fail_next: true,
        };
        let mut evm = ctx.build_monad_with_inspector(inspector).with_precompiles(precompiles);
        evm.ctx().block.basefee = 0;

        let first_tx = TxEnv::builder()
            .caller(caller)
            .kind(TxKind::Call(first))
            .gas_limit(1_000_000)
            .gas_price(0)
            .build_fill();
        assert!(evm.inspect_one_tx(first_tx).is_err());

        let mut cfg = evm.ctx().cfg.clone().into_inner();
        cfg.spec = parent_spec;
        evm.ctx().cfg = MonadCfgEnv::from(cfg);
        let second_tx = TxEnv::builder()
            .caller(caller)
            .kind(TxKind::Call(second))
            .gas_limit(1_000_000)
            .gas_price(0)
            .build_fill();
        let result = evm.inspect_one_tx(second_tx).expect("transaction after error should execute");
        assert!(matches!(result, ExecutionResult::Success { .. }));
        assert!(
            selected_specs
                .borrow()
                .windows(3)
                .any(|specs| specs == [parent_spec, child_spec, parent_spec]),
            "the next root frame should replace the failed child provider spec"
        );
    }

    #[test]
    #[cfg(feature = "memory_limit")]
    fn test_monad_nine_clamps_materialized_memory_limit_at_protocol_boundary() {
        let last_word_offset = MONAD_MEMORY_LIMIT as u32 - 32;
        let at_limit_offset = MONAD_MEMORY_LIMIT as u32;

        let below_limit = run_memory_limit_contract(last_word_offset);
        assert!(
            matches!(below_limit, ExecutionResult::Success { .. }),
            "the last word ending at 8 MiB should fit"
        );

        let above_limit = run_memory_limit_contract(at_limit_offset);
        assert!(
            matches!(
                above_limit,
                ExecutionResult::Halt {
                    reason: HaltReason::OutOfGas(OutOfGasError::MemoryLimit),
                    ..
                }
            ),
            "the first word ending above 8 MiB should exceed the memory limit"
        );
    }

    #[test]
    #[cfg(feature = "memory_limit")]
    fn test_memory_limit_follows_frame_spec_transitions() {
        let last_word_offset = MONAD_MEMORY_LIMIT as u32 - 32;
        let at_limit_offset = MONAD_MEMORY_LIMIT as u32;

        assert!(run_frame_memory_limit_transition(
            MonadHardfork::MonadEight,
            MonadHardfork::MonadNine,
            last_word_offset,
        ));
        assert!(!run_frame_memory_limit_transition(
            MonadHardfork::MonadEight,
            MonadHardfork::MonadNine,
            at_limit_offset,
        ));
        assert!(run_frame_memory_limit_transition(
            MonadHardfork::MonadNine,
            MonadHardfork::MonadEight,
            at_limit_offset,
        ));
    }

    #[test]
    fn test_clz_is_only_available_on_monad_nine() {
        let clz_contract = vec![
            opcode::PUSH1,
            0x01,
            opcode::CLZ,
            opcode::PUSH1,
            0x00,
            opcode::MSTORE,
            opcode::PUSH1,
            0x20,
            opcode::PUSH1,
            0x00,
            opcode::RETURN,
        ];

        let monad_eight_result = run_contract(MonadHardfork::MonadEight, clz_contract.clone());
        assert!(
            matches!(
                monad_eight_result,
                ExecutionResult::Halt { reason: HaltReason::NotActivated, .. }
            ),
            "CLZ should be unavailable before MonadNine, got {monad_eight_result:?}"
        );

        let monad_nine_result = run_contract(MonadHardfork::MonadNine, clz_contract);
        let output = monad_nine_result.output().expect("CLZ should return data on MonadNine");
        assert_eq!(
            U256::from_be_slice(output.as_ref()),
            U256::from(255),
            "CLZ(1) should return 255 on MonadNine"
        );
    }

    #[test]
    fn test_extended_stack_opcode_bytes_are_unavailable_on_monad_nine_and_next() {
        for spec in [MonadHardfork::MonadNine, MonadHardfork::MonadNext] {
            for opcode in [DUPN_OPCODE, SWAPN_OPCODE, EXCHANGE_OPCODE] {
                let result = run_contract(spec, vec![opcode]);
                assert!(
                    matches!(
                        result,
                        ExecutionResult::Halt {
                            reason: HaltReason::OpcodeNotFound | HaltReason::NotActivated,
                            ..
                        }
                    ),
                    "opcode 0x{opcode:02x} should be unavailable on {spec:?}, got {result:?}"
                );
            }
        }
    }

    #[test]
    fn test_jumpdest_after_unknown_extended_stack_opcode_byte_is_reachable() {
        let contract = vec![
            opcode::PUSH1,
            0x04,
            opcode::JUMP,
            DUPN_OPCODE,
            opcode::JUMPDEST,
            opcode::PUSH1,
            0x2a,
            opcode::PUSH1,
            0x00,
            opcode::MSTORE,
            opcode::PUSH1,
            0x20,
            opcode::PUSH1,
            0x00,
            opcode::RETURN,
        ];

        for spec in [MonadHardfork::MonadNine, MonadHardfork::MonadNext] {
            let result = run_contract(spec, contract.clone());
            let output = result.output().expect("jump target should execute successfully");
            assert_eq!(
                U256::from_be_slice(output.as_ref()),
                U256::from(42),
                "jumpdest after 0xE6 should remain reachable on {spec:?}"
            );
        }
    }

    #[test]
    fn test_create_is_rejected_for_delegated_accounts() {
        let delegated_address = Address::from([0x33; 20]);
        let delegated_code = vec![opcode::PUSH0, opcode::PUSH0, opcode::PUSH0, opcode::CREATE];

        for spec in [MonadHardfork::MonadEight, MonadHardfork::MonadNine, MonadHardfork::MonadNext]
        {
            let result = run_delegated_contract(
                spec,
                Bytecode::new_eip7702(delegated_address),
                delegated_address,
                delegated_code.clone(),
                &[],
            );
            assert!(
                matches!(
                    result,
                    ExecutionResult::Halt { reason: HaltReason::NotActivated, .. }
                ),
                "CREATE should halt with NotActivated for delegated accounts on {spec:?}, got {result:?}"
            );
        }
    }

    #[test]
    fn test_create2_is_rejected_for_delegated_accounts() {
        let delegated_address = Address::from([0x33; 20]);
        let delegated_code =
            vec![opcode::PUSH0, opcode::PUSH0, opcode::PUSH0, opcode::PUSH0, opcode::CREATE2];

        for spec in [MonadHardfork::MonadEight, MonadHardfork::MonadNine, MonadHardfork::MonadNext]
        {
            let result = run_delegated_contract(
                spec,
                Bytecode::new_eip7702(delegated_address),
                delegated_address,
                delegated_code.clone(),
                &[],
            );
            assert!(
                matches!(
                    result,
                    ExecutionResult::Halt { reason: HaltReason::NotActivated, .. }
                ),
                "CREATE2 should halt with NotActivated for delegated accounts on {spec:?}, got {result:?}"
            );
        }
    }

    #[test]
    fn test_nested_delegatecall_to_create2_only_fails_for_delegated_accounts() {
        let delegated_address = Address::from([0x33; 20]);
        let creator = Address::from([0x44; 20]);

        let mut delegated_code =
            vec![opcode::PUSH0, opcode::PUSH0, opcode::PUSH0, opcode::PUSH0, opcode::PUSH20];
        delegated_code.extend_from_slice(creator.as_slice());
        delegated_code.extend_from_slice(&[
            opcode::GAS,
            opcode::DELEGATECALL,
            opcode::PUSH1,
            0x1f,
            opcode::JUMPI,
            opcode::INVALID,
            opcode::JUMPDEST,
            opcode::STOP,
        ]);

        let creator_code = Bytecode::new_raw(Bytes::from(vec![
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::PUSH0,
            opcode::CREATE2,
        ]));

        for spec in [MonadHardfork::MonadEight, MonadHardfork::MonadNine, MonadHardfork::MonadNext]
        {
            let delegated_result = run_delegated_contract(
                spec,
                Bytecode::new_eip7702(delegated_address),
                delegated_address,
                delegated_code.clone(),
                &[(creator, creator_code.clone())],
            );
            assert!(
                matches!(
                    delegated_result,
                    ExecutionResult::Halt { reason: HaltReason::InvalidFEOpcode, .. }
                ),
                "nested delegatecall should hit the INVALID sentinel when delegated CREATE2 fails on {spec:?}, got {delegated_result:?}"
            );

            let regular_result = run_delegated_contract(
                spec,
                Bytecode::new_raw(Bytes::from(delegated_code.clone())),
                delegated_address,
                delegated_code.clone(),
                &[(creator, creator_code.clone())],
            );
            assert!(
                matches!(regular_result, ExecutionResult::Success { .. }),
                "nested delegatecall should succeed for a regular contract on {spec:?}, got {regular_result:?}"
            );
        }
    }

    #[test]
    fn test_top_level_delegated_staking_precompile_call_reverts() {
        let input = Bytes::from(getEpochCall::SELECTOR.to_vec());

        for spec in [MonadHardfork::MonadEight, MonadHardfork::MonadNine, MonadHardfork::MonadNext]
        {
            let result = run_contract_with_input_and_accounts(
                spec,
                Bytecode::new_eip7702(STAKING_ADDRESS),
                input.clone(),
                &[],
            );
            assert!(
                matches!(result, ExecutionResult::Revert { ref output, .. } if output.is_empty()),
                "delegated top-level staking call should revert with empty output on {spec:?}"
            );
        }
    }

    #[test]
    fn test_internal_call_to_delegated_staking_precompile_reverts() {
        let delegated_target = Address::from([0x55; 20]);
        let caller_code =
            call_returns_success_flag_contract(delegated_target, getEpochCall::SELECTOR);

        for spec in [MonadHardfork::MonadEight, MonadHardfork::MonadNine, MonadHardfork::MonadNext]
        {
            let result = run_contract_with_input_and_accounts(
                spec,
                Bytecode::new_raw(Bytes::from(caller_code.clone())),
                Bytes::new(),
                &[(delegated_target, Bytecode::new_eip7702(STAKING_ADDRESS))],
            );
            let output = result.output().expect("CALL result contract should return output");
            assert_eq!(
                U256::from_be_slice(output.as_ref()),
                U256::ZERO,
                "internal CALL into delegated staking precompile should fail on {spec:?}"
            );
        }
    }

    #[test]
    fn test_top_level_delegated_reserve_balance_precompile_call_reverts() {
        let input = Bytes::from(dippedIntoReserveCall::SELECTOR.to_vec());

        for spec in [MonadHardfork::MonadNine, MonadHardfork::MonadNext] {
            let result = run_contract_with_input_and_accounts(
                spec,
                Bytecode::new_eip7702(RESERVE_BALANCE_ADDRESS),
                input.clone(),
                &[],
            );
            assert!(
                matches!(result, ExecutionResult::Revert { ref output, .. } if output.is_empty()),
                "delegated top-level reserve-balance call should revert with empty output on {spec:?}"
            );
        }
    }

    #[test]
    fn test_internal_call_to_delegated_reserve_balance_precompile_reverts() {
        let delegated_target = Address::from([0x66; 20]);
        let caller_code =
            call_returns_success_flag_contract(delegated_target, dippedIntoReserveCall::SELECTOR);

        for spec in [MonadHardfork::MonadNine, MonadHardfork::MonadNext] {
            let result = run_contract_with_input_and_accounts(
                spec,
                Bytecode::new_raw(Bytes::from(caller_code.clone())),
                Bytes::new(),
                &[(delegated_target, Bytecode::new_eip7702(RESERVE_BALANCE_ADDRESS))],
            );
            let output = result.output().expect("CALL result contract should return output");
            assert_eq!(
                U256::from_be_slice(output.as_ref()),
                U256::ZERO,
                "internal CALL into delegated reserve-balance precompile should fail on {spec:?}"
            );
        }
    }

    #[test]
    fn test_create_still_succeeds_for_regular_contracts() {
        let result = run_contract(
            MonadHardfork::MonadNine,
            vec![opcode::PUSH0, opcode::PUSH0, opcode::PUSH0, opcode::CREATE, opcode::STOP],
        );
        assert!(
            matches!(result, ExecutionResult::Success { .. }),
            "regular CREATE should still succeed, got {result:?}"
        );
    }
}