1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//! Host interface for external blockchain state access.
use crate::{
cfg::GasParams,
context::{SStoreResult, SelfDestructResult, StateLoad},
journaled_state::{AccountInfoLoad, AccountLoad},
};
use auto_impl::auto_impl;
use primitives::{hardfork::SpecId, Address, Bytes, Log, StorageKey, StorageValue, B256, U256};
use state::Bytecode;
/// Error that can happen when loading account info.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LoadError {
/// Database error.
DBError,
/// Cold load skipped.
ColdLoadSkipped,
}
/// Host trait with all methods that are needed by the Interpreter.
///
/// This trait is implemented for all types that have `ContextTr` trait.
///
/// There are few groups of functions which are Block, Transaction, Config, Database and Journal functions.
#[auto_impl(&mut, Box)]
pub trait Host {
/* Block */
/// Block basefee, calls ContextTr::block().basefee()
fn basefee(&self) -> U256;
/// Block blob gasprice, calls `ContextTr::block().blob_gasprice()`
fn blob_gasprice(&self) -> U256;
/// Block gas limit, calls ContextTr::block().gas_limit()
fn gas_limit(&self) -> U256;
/// Block difficulty, calls ContextTr::block().difficulty()
fn difficulty(&self) -> U256;
/// Block prevrandao, calls ContextTr::block().prevrandao()
fn prevrandao(&self) -> Option<U256>;
/// Block number, calls ContextTr::block().number()
fn block_number(&self) -> U256;
/// Block timestamp, calls ContextTr::block().timestamp()
fn timestamp(&self) -> U256;
/// Block beneficiary, calls ContextTr::block().beneficiary()
fn beneficiary(&self) -> Address;
/// Block slot number, calls ContextTr::block().slot_num()
fn slot_num(&self) -> U256;
/// Chain id, calls ContextTr::cfg().chain_id()
fn chain_id(&self) -> U256;
/* Transaction */
/// Transaction effective gas price, calls `ContextTr::tx().effective_gas_price(basefee as u128)`
fn effective_gas_price(&self) -> U256;
/// Transaction caller, calls `ContextTr::tx().caller()`
fn caller(&self) -> Address;
/// Transaction blob hash, calls `ContextTr::tx().blob_hash(number)`
fn blob_hash(&self, number: usize) -> Option<U256>;
/* Config */
/// Max initcode size, calls `ContextTr::cfg().max_code_size().saturating_mul(2)`
fn max_initcode_size(&self) -> usize;
/// Gas params contains the dynamic gas constants for the EVM.
fn gas_params(&self) -> &GasParams;
/// Returns whether state gas (EIP-8037) is enabled.
fn is_amsterdam_eip8037_enabled(&self) -> bool;
/* Database */
/// Block hash, calls `ContextTr::journal_mut().db().block_hash(number)`
fn block_hash(&mut self, number: u64) -> Option<B256>;
/* Journal */
/// Selfdestruct account, calls `ContextTr::journal_mut().selfdestruct(address, target)`
fn selfdestruct(
&mut self,
address: Address,
target: Address,
skip_cold_load: bool,
) -> Result<StateLoad<SelfDestructResult>, LoadError>;
/// Log, calls `ContextTr::journal_mut().log(log)`
fn log(&mut self, log: Log);
/// Sstore with optional fetch from database. Return none if the value is cold or if there is db error.
fn sstore_skip_cold_load(
&mut self,
address: Address,
key: StorageKey,
value: StorageValue,
skip_cold_load: bool,
) -> Result<StateLoad<SStoreResult>, LoadError>;
/// Sstore, calls `ContextTr::journal_mut().sstore(address, key, value)`
fn sstore(
&mut self,
address: Address,
key: StorageKey,
value: StorageValue,
) -> Option<StateLoad<SStoreResult>> {
self.sstore_skip_cold_load(address, key, value, false).ok()
}
/// Sload with optional fetch from database. Return none if the value is cold or if there is db error.
fn sload_skip_cold_load(
&mut self,
address: Address,
key: StorageKey,
skip_cold_load: bool,
) -> Result<StateLoad<StorageValue>, LoadError>;
/// Sload, calls `ContextTr::journal_mut().sload(address, key)`
fn sload(&mut self, address: Address, key: StorageKey) -> Option<StateLoad<StorageValue>> {
self.sload_skip_cold_load(address, key, false).ok()
}
/// Tstore, calls `ContextTr::journal_mut().tstore(address, key, value)`
fn tstore(&mut self, address: Address, key: StorageKey, value: StorageValue);
/// Tload, calls `ContextTr::journal_mut().tload(address, key)`
fn tload(&mut self, address: Address, key: StorageKey) -> StorageValue;
/// Main function to load account info.
///
/// If load_code is true, it will load the code fetching it from the database if not done before.
///
/// If skip_cold_load is true, it will not load the account if it is cold. This is needed to short circuit
/// the load if there is not enough gas.
///
/// Returns AccountInfo, is_cold and is_empty.
fn load_account_info_skip_cold_load(
&mut self,
address: Address,
load_code: bool,
skip_cold_load: bool,
) -> Result<AccountInfoLoad<'_>, LoadError>;
/// Balance, calls `ContextTr::journal_mut().load_account(address)`
#[inline]
fn balance(&mut self, address: Address) -> Option<StateLoad<U256>> {
self.load_account_info_skip_cold_load(address, false, false)
.ok()
.map(|load| load.into_state_load(|i| i.balance))
}
/// Load account delegated, calls `ContextTr::journal_mut().load_account_delegated(address)`
#[inline]
fn load_account_delegated(&mut self, address: Address) -> Option<StateLoad<AccountLoad>> {
let account = self
.load_account_info_skip_cold_load(address, true, false)
.ok()?;
let mut account_load = StateLoad::new(
AccountLoad {
is_delegate_account_cold: None,
is_empty: account.is_empty,
},
account.is_cold,
);
// load delegate code if account is EIP-7702
if let Some(address) = account.code.as_ref().and_then(Bytecode::eip7702_address) {
let delegate_account = self
.load_account_info_skip_cold_load(address, true, false)
.ok()?;
account_load.data.is_delegate_account_cold = Some(delegate_account.is_cold);
account_load.data.is_empty = delegate_account.is_empty;
}
Some(account_load)
}
/// Load account code, calls [`Host::load_account_info_skip_cold_load`] with `load_code` set to false.
#[inline]
fn load_account_code(&mut self, address: Address) -> Option<StateLoad<Bytes>> {
self.load_account_info_skip_cold_load(address, true, false)
.ok()
.map(|load| {
load.into_state_load(|i| {
i.code
.as_ref()
.map(|b| b.original_bytes())
.unwrap_or_default()
})
})
}
/// Load account code hash, calls [`Host::load_account_info_skip_cold_load`] with `load_code` set to false.
#[inline]
fn load_account_code_hash(&mut self, address: Address) -> Option<StateLoad<B256>> {
self.load_account_info_skip_cold_load(address, false, false)
.ok()
.map(|load| {
load.into_state_load(|i| {
if i.is_empty() {
B256::ZERO
} else {
i.code_hash
}
})
})
}
}
/// Dummy host that implements [`Host`] trait and returns all default values.
#[derive(Default, Debug)]
pub struct DummyHost {
gas_params: GasParams,
}
impl DummyHost {
/// Create a new dummy host with the given spec.
pub fn new(spec: SpecId) -> Self {
Self {
gas_params: GasParams::new_spec(spec),
}
}
}
impl Host for DummyHost {
fn basefee(&self) -> U256 {
U256::ZERO
}
fn blob_gasprice(&self) -> U256 {
U256::ZERO
}
fn gas_limit(&self) -> U256 {
U256::ZERO
}
fn gas_params(&self) -> &GasParams {
&self.gas_params
}
fn is_amsterdam_eip8037_enabled(&self) -> bool {
false
}
fn difficulty(&self) -> U256 {
U256::ZERO
}
fn prevrandao(&self) -> Option<U256> {
None
}
fn block_number(&self) -> U256 {
U256::ZERO
}
fn timestamp(&self) -> U256 {
U256::ZERO
}
fn beneficiary(&self) -> Address {
Address::ZERO
}
fn slot_num(&self) -> U256 {
U256::ZERO
}
fn chain_id(&self) -> U256 {
U256::ZERO
}
fn effective_gas_price(&self) -> U256 {
U256::ZERO
}
fn caller(&self) -> Address {
Address::ZERO
}
fn blob_hash(&self, _number: usize) -> Option<U256> {
None
}
fn max_initcode_size(&self) -> usize {
0
}
fn block_hash(&mut self, _number: u64) -> Option<B256> {
None
}
fn selfdestruct(
&mut self,
_address: Address,
_target: Address,
_skip_cold_load: bool,
) -> Result<StateLoad<SelfDestructResult>, LoadError> {
Ok(Default::default())
}
fn log(&mut self, _log: Log) {}
fn tstore(&mut self, _address: Address, _key: StorageKey, _value: StorageValue) {}
fn tload(&mut self, _address: Address, _key: StorageKey) -> StorageValue {
StorageValue::ZERO
}
fn load_account_info_skip_cold_load(
&mut self,
_address: Address,
_load_code: bool,
_skip_cold_load: bool,
) -> Result<AccountInfoLoad<'_>, LoadError> {
Ok(Default::default())
}
fn sstore_skip_cold_load(
&mut self,
_address: Address,
_key: StorageKey,
_value: StorageValue,
_skip_cold_load: bool,
) -> Result<StateLoad<SStoreResult>, LoadError> {
Ok(Default::default())
}
fn sload_skip_cold_load(
&mut self,
_address: Address,
_key: StorageKey,
_skip_cold_load: bool,
) -> Result<StateLoad<StorageValue>, LoadError> {
Ok(Default::default())
}
}