revm_context_interface/
host.rs

1//! Host interface for external blockchain state access.
2
3use crate::{
4    context::{SStoreResult, SelfDestructResult, StateLoad},
5    journaled_state::AccountLoad,
6};
7use auto_impl::auto_impl;
8use primitives::{Address, Bytes, Log, StorageKey, StorageValue, B256, U256};
9
10/// Host trait with all methods that are needed by the Interpreter.
11///
12/// This trait is implemented for all types that have `ContextTr` trait.
13///
14/// There are few groups of functions which are Block, Transaction, Config, Database and Journal functions.
15#[auto_impl(&mut, Box)]
16pub trait Host {
17    /* Block */
18
19    /// Block basefee, calls ContextTr::block().basefee()
20    fn basefee(&self) -> U256;
21    /// Block blob gasprice, calls `ContextTr::block().blob_gasprice()`
22    fn blob_gasprice(&self) -> U256;
23    /// Block gas limit, calls ContextTr::block().gas_limit()
24    fn gas_limit(&self) -> U256;
25    /// Block difficulty, calls ContextTr::block().difficulty()
26    fn difficulty(&self) -> U256;
27    /// Block prevrandao, calls ContextTr::block().prevrandao()
28    fn prevrandao(&self) -> Option<U256>;
29    /// Block number, calls ContextTr::block().number()
30    fn block_number(&self) -> U256;
31    /// Block timestamp, calls ContextTr::block().timestamp()
32    fn timestamp(&self) -> U256;
33    /// Block beneficiary, calls ContextTr::block().beneficiary()
34    fn beneficiary(&self) -> Address;
35    /// Chain id, calls ContextTr::cfg().chain_id()
36    fn chain_id(&self) -> U256;
37
38    /* Transaction */
39
40    /// Transaction effective gas price, calls `ContextTr::tx().effective_gas_price(basefee as u128)`
41    fn effective_gas_price(&self) -> U256;
42    /// Transaction caller, calls `ContextTr::tx().caller()`
43    fn caller(&self) -> Address;
44    /// Transaction blob hash, calls `ContextTr::tx().blob_hash(number)`
45    fn blob_hash(&self, number: usize) -> Option<U256>;
46
47    /* Config */
48
49    /// Max initcode size, calls `ContextTr::cfg().max_code_size().saturating_mul(2)`
50    fn max_initcode_size(&self) -> usize;
51
52    /* Database */
53
54    /// Block hash, calls `ContextTr::journal_mut().db().block_hash(number)`
55    fn block_hash(&mut self, number: u64) -> Option<B256>;
56
57    /* Journal */
58
59    /// Selfdestruct account, calls `ContextTr::journal_mut().selfdestruct(address, target)`
60    fn selfdestruct(
61        &mut self,
62        address: Address,
63        target: Address,
64    ) -> Option<StateLoad<SelfDestructResult>>;
65
66    /// Log, calls `ContextTr::journal_mut().log(log)`
67    fn log(&mut self, log: Log);
68    /// Sstore, calls `ContextTr::journal_mut().sstore(address, key, value)`
69    fn sstore(
70        &mut self,
71        address: Address,
72        key: StorageKey,
73        value: StorageValue,
74    ) -> Option<StateLoad<SStoreResult>>;
75
76    /// Sload, calls `ContextTr::journal_mut().sload(address, key)`
77    fn sload(&mut self, address: Address, key: StorageKey) -> Option<StateLoad<StorageValue>>;
78    /// Tstore, calls `ContextTr::journal_mut().tstore(address, key, value)`
79    fn tstore(&mut self, address: Address, key: StorageKey, value: StorageValue);
80    /// Tload, calls `ContextTr::journal_mut().tload(address, key)`
81    fn tload(&mut self, address: Address, key: StorageKey) -> StorageValue;
82    /// Balance, calls `ContextTr::journal_mut().load_account(address)`
83    fn balance(&mut self, address: Address) -> Option<StateLoad<U256>>;
84    /// Load account delegated, calls `ContextTr::journal_mut().load_account_delegated(address)`
85    fn load_account_delegated(&mut self, address: Address) -> Option<StateLoad<AccountLoad>>;
86    /// Load account code, calls `ContextTr::journal_mut().load_account_code(address)`
87    fn load_account_code(&mut self, address: Address) -> Option<StateLoad<Bytes>>;
88    /// Load account code hash, calls `ContextTr::journal_mut().code_hash(address)`
89    fn load_account_code_hash(&mut self, address: Address) -> Option<StateLoad<B256>>;
90}
91
92/// Dummy host that implements [`Host`] trait and  returns all default values.
93#[derive(Debug)]
94pub struct DummyHost;
95
96impl Host for DummyHost {
97    fn basefee(&self) -> U256 {
98        U256::ZERO
99    }
100
101    fn blob_gasprice(&self) -> U256 {
102        U256::ZERO
103    }
104
105    fn gas_limit(&self) -> U256 {
106        U256::ZERO
107    }
108
109    fn difficulty(&self) -> U256 {
110        U256::ZERO
111    }
112
113    fn prevrandao(&self) -> Option<U256> {
114        None
115    }
116
117    fn block_number(&self) -> U256 {
118        U256::ZERO
119    }
120
121    fn timestamp(&self) -> U256 {
122        U256::ZERO
123    }
124
125    fn beneficiary(&self) -> Address {
126        Address::ZERO
127    }
128
129    fn chain_id(&self) -> U256 {
130        U256::ZERO
131    }
132
133    fn effective_gas_price(&self) -> U256 {
134        U256::ZERO
135    }
136
137    fn caller(&self) -> Address {
138        Address::ZERO
139    }
140
141    fn blob_hash(&self, _number: usize) -> Option<U256> {
142        None
143    }
144
145    fn max_initcode_size(&self) -> usize {
146        0
147    }
148
149    fn block_hash(&mut self, _number: u64) -> Option<B256> {
150        None
151    }
152
153    fn selfdestruct(
154        &mut self,
155        _address: Address,
156        _target: Address,
157    ) -> Option<StateLoad<SelfDestructResult>> {
158        None
159    }
160
161    fn log(&mut self, _log: Log) {}
162
163    fn sstore(
164        &mut self,
165        _address: Address,
166        _key: StorageKey,
167        _value: StorageValue,
168    ) -> Option<StateLoad<SStoreResult>> {
169        None
170    }
171
172    fn sload(&mut self, _address: Address, _key: StorageKey) -> Option<StateLoad<StorageValue>> {
173        None
174    }
175
176    fn tstore(&mut self, _address: Address, _key: StorageKey, _value: StorageValue) {}
177
178    fn tload(&mut self, _address: Address, _key: StorageKey) -> StorageValue {
179        StorageValue::ZERO
180    }
181
182    fn balance(&mut self, _address: Address) -> Option<StateLoad<U256>> {
183        None
184    }
185
186    fn load_account_delegated(&mut self, _address: Address) -> Option<StateLoad<AccountLoad>> {
187        None
188    }
189
190    fn load_account_code(&mut self, _address: Address) -> Option<StateLoad<Bytes>> {
191        None
192    }
193
194    fn load_account_code_hash(&mut self, _address: Address) -> Option<StateLoad<B256>> {
195        None
196    }
197}