revm_context_interface/
context.rs

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