monad-revm 0.3.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
use crate::MonadSpecId;
use revm::{
    context_interface::cfg::{GasId, GasParams},
    handler::instructions::EthInstructions,
    interpreter::{
        instructions::{instruction_table_gas_changes_spec, Instruction},
        interpreter::EthInterpreter,
        Host,
    },
};

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

/// 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: MonadSpecId) -> GasParams {
    let eth_spec = spec.into_eth_spec();
    let mut params = GasParams::new_spec(eth_spec);

    if MonadSpecId::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: MonadSpecId) -> MonadInstructions<CTX> {
    let eth_spec = spec.into_eth_spec();
    let mut instructions =
        EthInstructions::new(instruction_table_gas_changes_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));

    // MIP-3: Replace memory-expanding opcodes with linear-cost variants.
    if MonadSpecId::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));

        // 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),
        );

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

        // Call opcodes
        instructions
            .insert_instruction(CALL, Instruction::new(opcodes::call, gas::WARM_STORAGE_READ_COST));
        instructions.insert_instruction(
            CALLCODE,
            Instruction::new(opcodes::call_code, gas::WARM_STORAGE_READ_COST),
        );
        instructions.insert_instruction(
            DELEGATECALL,
            Instruction::new(opcodes::delegate_call, gas::WARM_STORAGE_READ_COST),
        );
        instructions.insert_instruction(
            STATICCALL,
            Instruction::new(opcodes::static_call, gas::WARM_STORAGE_READ_COST),
        );

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

    instructions
}

/// 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::*;
    use crate::{
        api::{builder::MonadBuilder, default_ctx::monad_context_with_db},
        MonadCfgEnv,
    };
    use revm::primitives::hardfork::SpecId;
    use revm::{
        bytecode::opcode,
        context::TxEnv,
        context_interface::result::{ExecutionResult, HaltReason},
        database::InMemoryDB,
        handler::EvmTr,
        primitives::{Address, Bytes, TxKind, U256},
        state::{AccountInfo, Bytecode},
        ExecuteEvm,
    };

    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(MonadSpecId::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(MonadSpecId::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(MonadSpecId::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(MonadSpecId::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(MonadSpecId::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: MonadSpecId, 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: MonadSpecId,
        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
    }

    #[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(MonadSpecId::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(MonadSpecId::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_unknown_on_monad_nine_and_next() {
        for spec in [MonadSpecId::MonadNine, MonadSpecId::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, .. }
                    ),
                    "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 [MonadSpecId::MonadNine, MonadSpecId::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 [MonadSpecId::MonadEight, MonadSpecId::MonadNine, MonadSpecId::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 [MonadSpecId::MonadEight, MonadSpecId::MonadNine, MonadSpecId::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 [MonadSpecId::MonadEight, MonadSpecId::MonadNine, MonadSpecId::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_create_still_succeeds_for_regular_contracts() {
        let result = run_contract(
            MonadSpecId::MonadNine,
            vec![opcode::PUSH0, opcode::PUSH0, opcode::PUSH0, opcode::CREATE, opcode::STOP],
        );
        assert!(
            matches!(result, ExecutionResult::Success { .. }),
            "regular CREATE should still succeed, got {result:?}"
        );
    }
}