revm_context_interface/
context.rs

1//! Context trait and related types.
2pub use crate::journaled_state::StateLoad;
3use crate::{Block, Cfg, Database, JournalTr, LocalContextTr, Transaction};
4use auto_impl::auto_impl;
5use primitives::StorageValue;
6use std::string::String;
7
8/// Trait that defines the context of the EVM execution.
9///
10/// This trait is used to access the environment and state of the EVM.
11/// It is used to access the transaction, block, configuration, database, journal, and chain.
12/// It is also used to set the error of the EVM.
13///
14/// All function has a `*_mut` variant except the function for [`ContextTr::tx`] and [`ContextTr::block`].
15#[auto_impl(&mut, Box)]
16pub trait ContextTr {
17    /// Block type
18    type Block: Block;
19    /// Transaction type
20    type Tx: Transaction;
21    /// Configuration type
22    type Cfg: Cfg;
23    /// Database type
24    type Db: Database;
25    /// Journal type
26    type Journal: JournalTr<Database = Self::Db>;
27    /// Chain type
28    type Chain;
29    /// Local context type
30    type Local: LocalContextTr;
31
32    /// Get the transaction
33    fn tx(&self) -> &Self::Tx;
34    /// Get the block
35    fn block(&self) -> &Self::Block;
36    /// Get the configuration
37    fn cfg(&self) -> &Self::Cfg;
38    /// Get the journal
39    fn journal(&self) -> &Self::Journal;
40    /// Get the journal mutably
41    fn journal_mut(&mut self) -> &mut Self::Journal;
42    /// Get the journal reference
43    fn journal_ref(&self) -> &Self::Journal {
44        self.journal()
45    }
46    /// Get the database
47    fn db(&self) -> &Self::Db;
48    /// Get the database mutably
49    fn db_mut(&mut self) -> &mut Self::Db;
50    /// Get the database reference
51    fn db_ref(&self) -> &Self::Db {
52        self.db()
53    }
54    /// Get the chain
55    fn chain(&self) -> &Self::Chain;
56    /// Get the chain mutably
57    fn chain_mut(&mut self) -> &mut Self::Chain;
58    /// Get the chain reference
59    fn chain_ref(&self) -> &Self::Chain {
60        self.chain()
61    }
62    /// Get the local context
63    fn local(&self) -> &Self::Local;
64    /// Get the local context mutably
65    fn local_mut(&mut self) -> &mut Self::Local;
66    /// Get the local context reference
67    fn local_ref(&self) -> &Self::Local {
68        self.local()
69    }
70    /// Get the error
71    fn error(&mut self) -> &mut Result<(), ContextError<<Self::Db as Database>::Error>>;
72    /// Get the transaction and journal. It is used to efficiently load access list
73    /// into journal without copying them from transaction.
74    fn tx_journal_mut(&mut self) -> (&Self::Tx, &mut Self::Journal);
75    /// Get the transaction and local context. It is used to efficiently load initcode
76    /// into local context without copying them from transaction.
77    fn tx_local_mut(&mut self) -> (&Self::Tx, &mut Self::Local);
78}
79
80/// Inner Context error used for Interpreter to set error without returning it frm instruction
81#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
82#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
83pub enum ContextError<DbError> {
84    /// Database error.
85    Db(DbError),
86    /// Custom string error.
87    Custom(String),
88}
89
90impl<DbError> From<DbError> for ContextError<DbError> {
91    fn from(value: DbError) -> Self {
92        Self::Db(value)
93    }
94}
95
96/// Represents the result of an `sstore` operation.
97#[derive(Clone, Debug, Default, PartialEq, Eq)]
98#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
99pub struct SStoreResult {
100    /// Value of the storage when it is first read
101    pub original_value: StorageValue,
102    /// Current value of the storage
103    pub present_value: StorageValue,
104    /// New value that is set
105    pub new_value: StorageValue,
106}
107
108impl SStoreResult {
109    /// Returns `true` if the new value is equal to the present value.
110    #[inline]
111    pub fn is_new_eq_present(&self) -> bool {
112        self.new_value == self.present_value
113    }
114
115    /// Returns `true` if the original value is equal to the present value.
116    #[inline]
117    pub fn is_original_eq_present(&self) -> bool {
118        self.original_value == self.present_value
119    }
120
121    /// Returns `true` if the original value is equal to the new value.
122    #[inline]
123    pub fn is_original_eq_new(&self) -> bool {
124        self.original_value == self.new_value
125    }
126
127    /// Returns `true` if the original value is zero.
128    #[inline]
129    pub fn is_original_zero(&self) -> bool {
130        self.original_value.is_zero()
131    }
132
133    /// Returns `true` if the present value is zero.
134    #[inline]
135    pub fn is_present_zero(&self) -> bool {
136        self.present_value.is_zero()
137    }
138
139    /// Returns `true` if the new value is zero.
140    #[inline]
141    pub fn is_new_zero(&self) -> bool {
142        self.new_value.is_zero()
143    }
144}
145
146/// Result of a selfdestruct action
147///
148/// Value returned are needed to calculate the gas spent.
149#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
150#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
151pub struct SelfDestructResult {
152    /// Whether the account had a value.
153    pub had_value: bool,
154    /// Whether the target account exists.
155    pub target_exists: bool,
156    /// Whether the account was previously destroyed.
157    pub previously_destroyed: bool,
158}
159
160/// Trait for setting the transaction and block in the context.
161pub trait ContextSetters: ContextTr {
162    /// Set the transaction
163    fn set_tx(&mut self, tx: Self::Tx);
164    /// Set the block
165    fn set_block(&mut self, block: Self::Block);
166}