rtvm_interpreter/
host.rs

1use crate::primitives::{Address, Bytecode, Env, Log, B256, U256};
2
3mod dummy;
4pub use dummy::DummyHost;
5
6/// EVM context host.
7pub trait Host {
8    /// Returns a reference to the environment.
9    fn env(&self) -> &Env;
10
11    /// Returns a mutable reference to the environment.
12    fn env_mut(&mut self) -> &mut Env;
13
14    /// Load an account.
15    ///
16    /// Returns (is_cold, is_new_account)
17    fn load_account(&mut self, address: Address) -> Option<LoadAccountResult>;
18
19    /// Get the block hash of the given block `number`.
20    fn block_hash(&mut self, number: U256) -> Option<B256>;
21
22    /// Get balance of `address` and if the account is cold.
23    fn balance(&mut self, address: Address) -> Option<(U256, bool)>;
24
25    /// Get code of `address` and if the account is cold.
26    fn code(&mut self, address: Address) -> Option<(Bytecode, bool)>;
27
28    /// Get code hash of `address` and if the account is cold.
29    fn code_hash(&mut self, address: Address) -> Option<(B256, bool)>;
30
31    /// Get storage value of `address` at `index` and if the account is cold.
32    fn sload(&mut self, address: Address, index: U256) -> Option<(U256, bool)>;
33
34    /// Set storage value of account address at index.
35    ///
36    /// Returns (original, present, new, is_cold).
37    fn sstore(&mut self, address: Address, index: U256, value: U256) -> Option<SStoreResult>;
38
39    /// Get the transient storage value of `address` at `index`.
40    fn tload(&mut self, address: Address, index: U256) -> U256;
41
42    /// Set the transient storage value of `address` at `index`.
43    fn tstore(&mut self, address: Address, index: U256, value: U256);
44
45    /// Emit a log owned by `address` with given `LogData`.
46    fn log(&mut self, log: Log);
47
48    /// Mark `address` to be deleted, with funds transferred to `target`.
49    fn selfdestruct(&mut self, address: Address, target: Address) -> Option<SelfDestructResult>;
50}
51
52/// Represents the result of an `sstore` operation.
53#[derive(Debug, Clone, PartialEq, Eq)]
54#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
55pub struct SStoreResult {
56    /// Value of the storage when it is first read
57    pub original_value: U256,
58    /// Current value of the storage
59    pub present_value: U256,
60    /// New value that is set
61    pub new_value: U256,
62    /// Is storage slot loaded from database
63    pub is_cold: bool,
64}
65
66/// Result of the account load from Journal state.
67#[derive(Debug, Clone, Default, PartialEq, Eq)]
68pub struct LoadAccountResult {
69    /// Is account cold loaded
70    pub is_cold: bool,
71    /// Is account empty, if true account is not created.
72    pub is_empty: bool,
73}
74
75/// Result of a selfdestruct instruction.
76#[derive(Default, Clone, Debug, PartialEq, Eq, Hash)]
77#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
78pub struct SelfDestructResult {
79    pub had_value: bool,
80    pub target_exists: bool,
81    pub is_cold: bool,
82    pub previously_destroyed: bool,
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    fn assert_host<H: Host + ?Sized>() {}
90
91    #[test]
92    fn object_safety() {
93        assert_host::<DummyHost>();
94        assert_host::<dyn Host>();
95    }
96}