revm_context/
evm.rs

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