revm_context/
evm.rs

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