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 an immutable reference to the execution context
31    fn ctx_ref(&self) -> &Self::Context;
32
33    /// Returns mutable references to both the context and instruction set.
34    /// This enables atomic access to both components when needed.
35    fn ctx_instructions(&mut self) -> (&mut Self::Context, &mut Self::Instructions);
36
37    /// Returns mutable references to both the context and precompiles.
38    /// This enables atomic access to both components when needed.
39    fn ctx_precompiles(&mut self) -> (&mut Self::Context, &mut Self::Precompiles);
40}
41
42impl<CTX, INSP, I, P> EvmTr for Evm<CTX, INSP, I, P>
43where
44    CTX: ContextTr,
45    I: InstructionProvider<
46        Context = CTX,
47        InterpreterTypes: InterpreterTypes<Output = InterpreterAction>,
48    >,
49    P: PrecompileProvider<CTX>,
50{
51    type Context = CTX;
52    type Instructions = I;
53    type Precompiles = P;
54
55    #[inline]
56    fn run_interpreter(
57        &mut self,
58        interpreter: &mut Interpreter<
59            <Self::Instructions as InstructionProvider>::InterpreterTypes,
60        >,
61    ) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output
62    {
63        let context = &mut self.ctx;
64        let instructions = &mut self.instruction;
65        interpreter.run_plain(instructions.instruction_table(), context)
66    }
67    #[inline]
68    fn ctx(&mut self) -> &mut Self::Context {
69        &mut self.ctx
70    }
71
72    #[inline]
73    fn ctx_ref(&self) -> &Self::Context {
74        &self.ctx
75    }
76
77    #[inline]
78    fn ctx_instructions(&mut self) -> (&mut Self::Context, &mut Self::Instructions) {
79        (&mut self.ctx, &mut self.instruction)
80    }
81
82    #[inline]
83    fn ctx_precompiles(&mut self) -> (&mut Self::Context, &mut Self::Precompiles) {
84        (&mut self.ctx, &mut self.precompiles)
85    }
86}