revm_handler/
evm.rs

1use crate::{instructions::InstructionProvider, PrecompileProvider};
2use auto_impl::auto_impl;
3use context::{ContextTr, Evm};
4use interpreter::{Interpreter, InterpreterAction, InterpreterTypes};
5
6/// A trait that integrates context, instruction set, and precompiles to create an EVM struct.
7///
8/// In addition to execution capabilities, this trait provides getter methods for its component fields.
9#[auto_impl(&mut, Box)]
10pub trait EvmTr {
11    /// The context type that implements ContextTr to provide access to execution state
12    type Context: ContextTr;
13    /// The instruction set type that implements InstructionProvider to define available operations
14    type Instructions: InstructionProvider;
15    /// The type containing the available precompiled contracts
16    type Precompiles: PrecompileProvider<Self::Context>;
17
18    /// Executes the interpreter loop for the given interpreter instance.
19    /// Returns either a completion status or the next interpreter action to take.
20    fn run_interpreter(
21        &mut self,
22        interpreter: &mut Interpreter<
23            <Self::Instructions as InstructionProvider>::InterpreterTypes,
24        >,
25    ) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output;
26
27    /// Returns a mutable reference to the execution context
28    fn ctx(&mut self) -> &mut Self::Context;
29
30    /// Returns a mutable reference to the execution context
31    fn ctx_mut(&mut self) -> &mut Self::Context {
32        self.ctx()
33    }
34
35    /// Returns an immutable reference to the execution context
36    fn ctx_ref(&self) -> &Self::Context;
37
38    /// Returns mutable references to both the context and instruction set.
39    /// This enables atomic access to both components when needed.
40    fn ctx_instructions(&mut self) -> (&mut Self::Context, &mut Self::Instructions);
41
42    /// Returns mutable references to both the context and precompiles.
43    /// This enables atomic access to both components when needed.
44    fn ctx_precompiles(&mut self) -> (&mut Self::Context, &mut Self::Precompiles);
45}
46
47impl<CTX, INSP, I, P> EvmTr for Evm<CTX, INSP, I, P>
48where
49    CTX: ContextTr,
50    I: InstructionProvider<
51        Context = CTX,
52        InterpreterTypes: InterpreterTypes<Output = InterpreterAction>,
53    >,
54    P: PrecompileProvider<CTX>,
55{
56    type Context = CTX;
57    type Instructions = I;
58    type Precompiles = P;
59
60    #[inline]
61    fn run_interpreter(
62        &mut self,
63        interpreter: &mut Interpreter<
64            <Self::Instructions as InstructionProvider>::InterpreterTypes,
65        >,
66    ) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output
67    {
68        let context = &mut self.ctx;
69        let instructions = &mut self.instruction;
70        interpreter.run_plain(instructions.instruction_table(), context)
71    }
72    #[inline]
73    fn ctx(&mut self) -> &mut Self::Context {
74        &mut self.ctx
75    }
76
77    #[inline]
78    fn ctx_ref(&self) -> &Self::Context {
79        &self.ctx
80    }
81
82    #[inline]
83    fn ctx_instructions(&mut self) -> (&mut Self::Context, &mut Self::Instructions) {
84        (&mut self.ctx, &mut self.instruction)
85    }
86
87    #[inline]
88    fn ctx_precompiles(&mut self) -> (&mut Self::Context, &mut Self::Precompiles) {
89        (&mut self.ctx, &mut self.precompiles)
90    }
91}