op_revm/
evm.rs

1//! Contains the `[OpEvm]` type and its implementation of the execution EVM traits.
2use crate::precompiles::OpPrecompiles;
3use revm::{
4    context::{ContextSetters, Evm},
5    context_interface::ContextTr,
6    handler::{
7        instructions::{EthInstructions, InstructionProvider},
8        EvmTr, PrecompileProvider,
9    },
10    inspector::{InspectorEvmTr, JournalExt},
11    interpreter::{interpreter::EthInterpreter, Interpreter, InterpreterAction, InterpreterTypes},
12    Inspector,
13};
14
15/// Optimism EVM extends the [`Evm`] type with Optimism specific types and logic.
16#[derive(Debug, Clone)]
17pub struct OpEvm<CTX, INSP, I = EthInstructions<EthInterpreter, CTX>, P = OpPrecompiles>(
18    /// Inner EVM type.
19    pub Evm<CTX, INSP, I, P>,
20);
21
22impl<CTX: ContextTr, INSP> OpEvm<CTX, INSP, EthInstructions<EthInterpreter, CTX>, OpPrecompiles> {
23    /// Create a new Optimism EVM.
24    pub fn new(ctx: CTX, inspector: INSP) -> Self {
25        Self(Evm {
26            ctx,
27            inspector,
28            instruction: EthInstructions::new_mainnet(),
29            precompiles: OpPrecompiles::default(),
30        })
31    }
32}
33
34impl<CTX, INSP, I, P> OpEvm<CTX, INSP, I, P> {
35    /// Consumed self and returns a new Evm type with given Inspector.
36    pub fn with_inspector<OINSP>(self, inspector: OINSP) -> OpEvm<CTX, OINSP, I, P> {
37        OpEvm(self.0.with_inspector(inspector))
38    }
39
40    /// Consumes self and returns a new Evm type with given Precompiles.
41    pub fn with_precompiles<OP>(self, precompiles: OP) -> OpEvm<CTX, INSP, I, OP> {
42        OpEvm(self.0.with_precompiles(precompiles))
43    }
44
45    /// Consumes self and returns the inner Inspector.
46    pub fn into_inspector(self) -> INSP {
47        self.0.into_inspector()
48    }
49}
50
51impl<CTX, INSP, I, P> InspectorEvmTr for OpEvm<CTX, INSP, I, P>
52where
53    CTX: ContextTr<Journal: JournalExt> + ContextSetters,
54    I: InstructionProvider<
55        Context = CTX,
56        InterpreterTypes: InterpreterTypes<Output = InterpreterAction>,
57    >,
58    P: PrecompileProvider<CTX>,
59    INSP: Inspector<CTX, I::InterpreterTypes>,
60{
61    type Inspector = INSP;
62
63    fn inspector(&mut self) -> &mut Self::Inspector {
64        &mut self.0.inspector
65    }
66
67    fn ctx_inspector(&mut self) -> (&mut Self::Context, &mut Self::Inspector) {
68        (&mut self.0.ctx, &mut self.0.inspector)
69    }
70
71    fn run_inspect_interpreter(
72        &mut self,
73        interpreter: &mut Interpreter<
74            <Self::Instructions as InstructionProvider>::InterpreterTypes,
75        >,
76    ) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output
77    {
78        self.0.run_inspect_interpreter(interpreter)
79    }
80}
81
82impl<CTX, INSP, I, P> EvmTr for OpEvm<CTX, INSP, I, P>
83where
84    CTX: ContextTr,
85    I: InstructionProvider<
86        Context = CTX,
87        InterpreterTypes: InterpreterTypes<Output = InterpreterAction>,
88    >,
89    P: PrecompileProvider<CTX>,
90{
91    type Context = CTX;
92    type Instructions = I;
93    type Precompiles = P;
94
95    fn run_interpreter(
96        &mut self,
97        interpreter: &mut Interpreter<
98            <Self::Instructions as InstructionProvider>::InterpreterTypes,
99        >,
100    ) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output
101    {
102        let context = &mut self.0.ctx;
103        let instructions = &mut self.0.instruction;
104        interpreter.run_plain(instructions.instruction_table(), context)
105    }
106
107    fn ctx(&mut self) -> &mut Self::Context {
108        &mut self.0.ctx
109    }
110
111    fn ctx_ref(&self) -> &Self::Context {
112        &self.0.ctx
113    }
114
115    fn ctx_instructions(&mut self) -> (&mut Self::Context, &mut Self::Instructions) {
116        (&mut self.0.ctx, &mut self.0.instruction)
117    }
118
119    fn ctx_precompiles(&mut self) -> (&mut Self::Context, &mut Self::Precompiles) {
120        (&mut self.0.ctx, &mut self.0.precompiles)
121    }
122}