revm_context_interface/
context.rs

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