revm_context/
evm.rs

1//! This module contains [`Evm`] struct.
2use core::fmt::Debug;
3use core::ops::{Deref, DerefMut};
4
5use context_interface::FrameStack;
6
7/// Main EVM structure that contains all data needed for execution.
8#[derive(Debug, Clone)]
9pub struct Evm<CTX, INSP, I, P, F> {
10    /// [`context_interface::ContextTr`] of the EVM it is used to fetch data from database.
11    pub ctx: CTX,
12    /// Inspector of the EVM it is used to inspect the EVM.
13    /// Its trait are defined in revm-inspector crate.
14    pub inspector: INSP,
15    /// Instructions provider of the EVM it is used to execute instructions.
16    /// `InstructionProvider` trait is defined in revm-handler crate.
17    pub instruction: I,
18    /// Precompile provider of the EVM it is used to execute precompiles.
19    /// `PrecompileProvider` trait is defined in revm-handler crate.
20    pub precompiles: P,
21    /// Frame that is going to be executed.
22    pub frame_stack: FrameStack<F>,
23}
24
25impl<CTX, I, P, F> Evm<CTX, (), I, P, F> {
26    /// Create a new EVM instance with a given context, instruction set, and precompile provider.
27    ///
28    /// Inspector will be set to `()`.
29    pub fn new(ctx: CTX, instruction: I, precompiles: P) -> Self {
30        Evm {
31            ctx,
32            inspector: (),
33            instruction,
34            precompiles,
35            frame_stack: FrameStack::new(),
36        }
37    }
38}
39
40impl<CTX, I, INSP, P, F> Evm<CTX, INSP, I, P, F> {
41    /// Create a new EVM instance with a given context, inspector, instruction set, and precompile provider.
42    pub fn new_with_inspector(ctx: CTX, inspector: INSP, instruction: I, precompiles: P) -> Self {
43        Evm {
44            ctx,
45            inspector,
46            instruction,
47            precompiles,
48            frame_stack: FrameStack::new(),
49        }
50    }
51}
52
53impl<CTX, INSP, I, P, F> Evm<CTX, INSP, I, P, F> {
54    /// Consumed self and returns new Evm type with given Inspector.
55    pub fn with_inspector<OINSP>(self, inspector: OINSP) -> Evm<CTX, OINSP, I, P, F> {
56        Evm {
57            ctx: self.ctx,
58            inspector,
59
60            instruction: self.instruction,
61            precompiles: self.precompiles,
62            frame_stack: self.frame_stack,
63        }
64    }
65
66    /// Consumes self and returns new Evm type with given Precompiles.
67    pub fn with_precompiles<OP>(self, precompiles: OP) -> Evm<CTX, INSP, I, OP, F> {
68        Evm {
69            ctx: self.ctx,
70            inspector: self.inspector,
71            instruction: self.instruction,
72            precompiles,
73            frame_stack: self.frame_stack,
74        }
75    }
76
77    /// Consumes self and returns inner Inspector.
78    pub fn into_inspector(self) -> INSP {
79        self.inspector
80    }
81}
82
83impl<CTX, INSP, I, P, F> Deref for Evm<CTX, INSP, I, P, F> {
84    type Target = CTX;
85
86    fn deref(&self) -> &Self::Target {
87        &self.ctx
88    }
89}
90
91impl<CTX, INSP, I, P, F> DerefMut for Evm<CTX, INSP, I, P, F> {
92    fn deref_mut(&mut self) -> &mut Self::Target {
93        &mut self.ctx
94    }
95}