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
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,
]);
#[derive(Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AccountInfo {
pub balance: U256,
pub code_hash: H256,
pub code: Option<Bytes>,
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()
}
}
}
#[derive(Clone, Debug)]
pub enum TransactTo {
Call(H160),
Create(CreateScheme),
}
impl TransactTo {
pub fn create() -> Self {
Self::Create(CreateScheme::Create)
}
}
#[derive(Debug)]
pub enum TransactOut {
None,
Call(Bytes),
Create(Bytes, Option<H160>),
}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum CreateScheme {
Create,
Create2 {
salt: U256,
},
}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum CallScheme {
Call,
CallCode,
DelegateCall,
StaticCall,
}
#[derive(Clone, Debug, Default)]
pub struct CallContext {
pub address: H160,
pub caller: H160,
pub apparent_value: U256,
}
#[derive(Clone, Debug, Default)]
pub struct Env {
pub cfg: CfgEnv,
pub block: BlockEnv,
pub tx: TxEnv,
}
#[derive(Clone, Debug)]
pub struct BlockEnv {
pub number: U256,
pub coinbase: H160,
pub timestamp: U256,
pub difficulty: U256,
pub basefee: U256,
pub gas_limit: U256,
}
#[derive(Clone, Debug)]
pub struct TxEnv {
pub caller: H160,
pub gas_limit: u64,
pub gas_price: U256,
pub gas_priority_fee: Option<U256>,
pub transact_to: TransactTo,
pub value: U256,
pub data: Bytes,
pub chain_id: Option<u64>,
pub nonce: Option<u64>,
pub access_list: Vec<(H160, Vec<U256>)>,
}
#[derive(Clone, Debug)]
pub struct CfgEnv {
pub chain_id: U256,
pub spec_id: SpecId,
pub perf_all_precompiles_have_balance: bool,
}
impl Default for CfgEnv {
fn default() -> CfgEnv {
CfgEnv {
chain_id: 1.into(),
spec_id: SpecId::LATEST,
perf_all_precompiles_have_balance: false,
}
}
}
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()),
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(),
)
}
}
}
#[derive(Clone, Debug)]
pub struct Transfer {
pub source: H160,
pub target: H160,
pub value: U256,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Log {
pub address: H160,
pub topics: Vec<H256>,
pub data: Bytes,
}
#[derive(Default)]
pub struct SelfDestructResult {
pub had_value: bool,
pub exists: bool,
pub is_cold: bool,
pub previously_destroyed: bool,
}