/
models.rs
389 lines (354 loc) · 10.9 KB
/
models.rs
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use core::cmp::min;
use crate::{alloc::vec::Vec, SpecId};
use bytes::Bytes;
use primitive_types::{H160, H256, U256};
use sha3::{Digest, Keccak256};
pub const KECCAK_EMPTY: H256 = H256([
0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0,
0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70,
]);
/// AccountInfo account information.
#[derive(Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AccountInfo {
/// Account balance.
pub balance: U256,
/// code hash,
pub code_hash: H256,
/// code: if None, `code_by_hash` will be used to fetch it if code needs to be loaded from
/// inside of revm.
#[cfg_attr(feature = "with-serde", serde(with = "serde_hex_bytes_opt"))]
pub code: Option<Bytes>,
/// Account nonce.
pub nonce: u64,
}
impl Default for AccountInfo {
fn default() -> Self {
Self {
balance: U256::zero(),
code_hash: KECCAK_EMPTY,
code: Some(Bytes::new()),
nonce: 0,
}
}
}
impl AccountInfo {
pub fn new(balance: U256, nonce: u64, code: Bytes) -> Self {
let code_hash = if code.is_empty() {
KECCAK_EMPTY
} else {
H256::from_slice(Keccak256::digest(&code).as_slice())
};
Self {
balance,
nonce,
code: Some(code),
code_hash,
}
}
pub fn is_empty(&self) -> bool {
let code_empty = self.code_hash == KECCAK_EMPTY || self.code_hash.is_zero();
self.balance.is_zero() && self.nonce == 0 && code_empty
}
pub fn exists(&self) -> bool {
!self.is_empty()
}
pub fn from_balance(balance: U256) -> Self {
AccountInfo {
balance,
..Default::default()
}
}
}
/// Inputs for a call.
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CallInputs {
/// The target of the call.
pub contract: H160,
/// The transfer, if any, in this call.
pub transfer: Transfer,
/// The call data of the call.
#[cfg_attr(feature = "with-serde", serde(with = "serde_hex_bytes"))]
pub input: Bytes,
/// The gas limit of the call.
pub gas_limit: u64,
/// The context of the call.
pub context: CallContext,
}
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateInputs {
pub caller: H160,
pub scheme: CreateScheme,
pub value: U256,
#[cfg_attr(feature = "with-serde", serde(with = "serde_hex_bytes"))]
pub init_code: Bytes,
pub gas_limit: u64,
}
pub struct CreateData {}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TransactTo {
Call(H160),
Create(CreateScheme),
}
impl TransactTo {
pub fn create() -> Self {
Self::Create(CreateScheme::Create)
}
}
#[derive(Debug)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TransactOut {
None,
#[cfg_attr(feature = "with-serde", serde(with = "serde_hex_bytes"))]
Call(Bytes),
Create(
#[cfg_attr(feature = "with-serde", serde(with = "serde_hex_bytes"))] Bytes,
Option<H160>,
),
}
/// Create scheme.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CreateScheme {
/// Legacy create scheme of `CREATE`.
Create,
/// Create scheme of `CREATE2`.
Create2 {
/// Salt.
salt: U256,
},
}
/// Call schemes.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CallScheme {
/// `CALL`
Call,
/// `CALLCODE`
CallCode,
/// `DELEGATECALL`
DelegateCall,
/// `STATICCALL`
StaticCall,
}
/// CallContext of the runtime.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CallContext {
/// Execution address.
pub address: H160,
/// Caller of the EVM.
pub caller: H160,
/// The address the contract code was loaded from, if any.
pub code_address: H160,
/// Apparent value of the EVM.
pub apparent_value: U256,
/// The scheme used for the call.
pub scheme: CallScheme,
}
impl Default for CallContext {
fn default() -> Self {
CallContext {
address: H160::default(),
caller: H160::default(),
code_address: H160::default(),
apparent_value: U256::default(),
scheme: CallScheme::Call,
}
}
}
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Env {
pub cfg: CfgEnv,
pub block: BlockEnv,
pub tx: TxEnv,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockEnv {
pub number: U256,
/// Coinbase or miner or address that created and signed the block.
/// Address where we are going to send gas spend
pub coinbase: H160,
pub timestamp: U256,
pub difficulty: U256,
/// basefee is added in EIP1559 London upgrade
pub basefee: U256,
pub gas_limit: U256,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TxEnv {
/// Caller or Author or tx signer
pub caller: H160,
pub gas_limit: u64,
pub gas_price: U256,
pub gas_priority_fee: Option<U256>,
pub transact_to: TransactTo,
pub value: U256,
#[cfg_attr(feature = "with-serde", serde(with = "serde_hex_bytes"))]
pub data: Bytes,
pub chain_id: Option<u64>,
pub nonce: Option<u64>,
pub access_list: Vec<(H160, Vec<U256>)>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CfgEnv {
pub chain_id: U256,
pub spec_id: SpecId,
/// If all precompiles have some balance we can skip initially fetching them from the database.
/// This is is not really needed on mainnet, and defaults to false, but in most cases it is
/// safe to be set to `true`, depending on the chain.
pub perf_all_precompiles_have_balance: bool,
/// A hard memory limit in bytes beyond which [Memory] cannot be resized.
///
/// In cases where the gas limit may be extraordinarily high, it is recommended to set this to
/// a sane value to prevent memory allocation panics. Defaults to `2^32 - 1` bytes per
/// EIP-1985.
#[cfg(feature = "memory_limit")]
pub memory_limit: u64,
}
impl Default for CfgEnv {
fn default() -> CfgEnv {
CfgEnv {
chain_id: 1.into(),
spec_id: SpecId::LATEST,
perf_all_precompiles_have_balance: false,
#[cfg(feature = "memory_limit")]
memory_limit: 2u64.pow(32) - 1,
}
}
}
impl Default for BlockEnv {
fn default() -> BlockEnv {
BlockEnv {
gas_limit: U256::MAX,
number: 0.into(),
coinbase: H160::zero(),
timestamp: U256::one(),
difficulty: U256::zero(),
basefee: U256::zero(),
}
}
}
impl Default for TxEnv {
fn default() -> TxEnv {
TxEnv {
caller: H160::zero(),
gas_limit: u64::MAX,
gas_price: U256::zero(),
gas_priority_fee: None,
transact_to: TransactTo::Call(H160::zero()), //will do nothing
value: U256::zero(),
data: Bytes::new(),
chain_id: None,
nonce: None,
access_list: Vec::new(),
}
}
}
impl Env {
pub fn effective_gas_price(&self) -> U256 {
if self.tx.gas_priority_fee.is_none() {
self.tx.gas_price
} else {
min(
self.tx.gas_price,
self.block.basefee + self.tx.gas_priority_fee.unwrap(),
)
}
}
}
/// Transfer from source to target, with given value.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Transfer {
/// Source address.
pub source: H160,
/// Target address.
pub target: H160,
/// Transfer value.
pub value: U256,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Log {
pub address: H160,
pub topics: Vec<H256>,
#[cfg_attr(feature = "with-serde", serde(with = "serde_hex_bytes"))]
pub data: Bytes,
}
#[derive(Default)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SelfDestructResult {
pub had_value: bool,
pub exists: bool,
pub is_cold: bool,
pub previously_destroyed: bool,
}
/// Serde functions to serde as [bytes::Bytes] hex string
#[cfg(feature = "with-serde")]
mod serde_hex_bytes {
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S, T>(x: T, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<[u8]>,
{
s.serialize_str(&format!("0x{}", hex::encode(x.as_ref())))
}
pub fn deserialize<'de, D>(d: D) -> Result<bytes::Bytes, D::Error>
where
D: Deserializer<'de>,
{
let value = String::deserialize(d)?;
if let Some(value) = value.strip_prefix("0x") {
hex::decode(value)
} else {
hex::decode(&value)
}
.map(Into::into)
.map_err(|e| serde::de::Error::custom(e.to_string()))
}
}
/// Serde functions to serde an Option [bytes::Bytes] hex string
#[cfg(feature = "with-serde")]
mod serde_hex_bytes_opt {
use super::serde_hex_bytes;
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<T, S>(value: &Option<T>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<[u8]>,
{
if let Some(value) = value {
serde_hex_bytes::serialize(value, serializer)
} else {
serializer.serialize_none()
}
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<bytes::Bytes>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(transparent)]
struct OptionalBytes(Option<DeserializeBytes>);
struct DeserializeBytes(bytes::Bytes);
impl<'de> Deserialize<'de> for DeserializeBytes {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Ok(DeserializeBytes(serde_hex_bytes::deserialize(
deserializer,
)?))
}
}
let value = OptionalBytes::deserialize(deserializer)?;
Ok(value.0.map(|b| b.0))
}
}