Skip to main content

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    /// Reusable async execution stack.
26    #[cfg(feature = "asyncdb")]
27    pub async_stack: database_interface::async_db::FiberStack,
28}
29
30impl<CTX, I, P, F: Default> Evm<CTX, (), I, P, F> {
31    /// Create a new EVM instance with a given context, instruction set, and precompile provider.
32    ///
33    /// Inspector will be set to `()`.
34    pub fn new(ctx: CTX, instruction: I, precompiles: P) -> Self {
35        Evm {
36            ctx,
37            inspector: (),
38            instruction,
39            precompiles,
40            frame_stack: FrameStack::new_prealloc(8),
41            #[cfg(feature = "asyncdb")]
42            async_stack: database_interface::async_db::FiberStack::default(),
43        }
44    }
45}
46
47impl<CTX, I, INSP, P, F: Default> Evm<CTX, INSP, I, P, F> {
48    /// Create a new EVM instance with a given context, inspector, instruction set, and precompile provider.
49    pub fn new_with_inspector(ctx: CTX, inspector: INSP, instruction: I, precompiles: P) -> Self {
50        Evm {
51            ctx,
52            inspector,
53            instruction,
54            precompiles,
55            frame_stack: FrameStack::new_prealloc(8),
56            #[cfg(feature = "asyncdb")]
57            async_stack: database_interface::async_db::FiberStack::default(),
58        }
59    }
60}
61
62impl<CTX, INSP, I, P, F> Evm<CTX, INSP, I, P, F> {
63    /// Consumed self and returns new Evm type with given Inspector.
64    pub fn with_inspector<OINSP>(self, inspector: OINSP) -> Evm<CTX, OINSP, I, P, F> {
65        Evm {
66            ctx: self.ctx,
67            inspector,
68
69            instruction: self.instruction,
70            precompiles: self.precompiles,
71            frame_stack: self.frame_stack,
72            #[cfg(feature = "asyncdb")]
73            async_stack: self.async_stack,
74        }
75    }
76
77    /// Consumes self and returns new Evm type with given Precompiles.
78    pub fn with_precompiles<OP>(self, precompiles: OP) -> Evm<CTX, INSP, I, OP, F> {
79        Evm {
80            ctx: self.ctx,
81            inspector: self.inspector,
82            instruction: self.instruction,
83            precompiles,
84            frame_stack: self.frame_stack,
85            #[cfg(feature = "asyncdb")]
86            async_stack: self.async_stack,
87        }
88    }
89
90    /// Consumes self and returns inner Inspector.
91    pub fn into_inspector(self) -> INSP {
92        self.inspector
93    }
94}
95
96impl<CTX, INSP, I, P, F> Deref for Evm<CTX, INSP, I, P, F> {
97    type Target = CTX;
98
99    fn deref(&self) -> &Self::Target {
100        &self.ctx
101    }
102}
103
104impl<CTX, INSP, I, P, F> DerefMut for Evm<CTX, INSP, I, P, F> {
105    fn deref_mut(&mut self) -> &mut Self::Target {
106        &mut self.ctx
107    }
108}