revm_context_interface/
context.rs

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