-
Notifications
You must be signed in to change notification settings - Fork 648
/
Copy pathenv.rs
352 lines (317 loc) · 12 KB
/
env.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
use crate::{
alloc::vec::Vec, Account, EVMError, InvalidTransaction, Spec, SpecId, B160, B256, KECCAK_EMPTY,
MAX_INITCODE_SIZE, U256,
};
use bytes::Bytes;
use core::cmp::{min, Ordering};
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "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 = "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: B160,
pub timestamp: U256,
/// Difficulty is removed and not used after Paris (aka TheMerge). Value is replaced with prevrandao.
pub difficulty: U256,
/// Prevrandao is used after Paris (aka TheMerge) instead of the difficulty value.
/// NOTE: prevrandao can be found in block in place of mix_hash.
pub prevrandao: Option<B256>,
/// basefee is added in EIP1559 London upgrade
pub basefee: U256,
pub gas_limit: U256,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TxEnv {
/// Caller or Author or tx signer
pub caller: B160,
pub gas_limit: u64,
pub gas_price: U256,
pub gas_priority_fee: Option<U256>,
pub transact_to: TransactTo,
pub value: U256,
#[cfg_attr(feature = "serde", serde(with = "crate::utilities::serde_hex_bytes"))]
pub data: Bytes,
pub chain_id: Option<u64>,
pub nonce: Option<u64>,
pub access_list: Vec<(B160, Vec<U256>)>,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TransactTo {
Call(B160),
Create(CreateScheme),
}
impl TransactTo {
pub fn create() -> Self {
Self::Create(CreateScheme::Create)
}
pub fn is_create(&self) -> bool {
matches!(self, Self::Create(_))
}
}
/// Create scheme.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CreateScheme {
/// Legacy create scheme of `CREATE`.
Create,
/// Create scheme of `CREATE2`.
Create2 {
/// Salt.
salt: U256,
},
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CfgEnv {
pub chain_id: U256,
pub spec_id: SpecId,
/// Bytecode that is created with CREATE/CREATE2 is by default analysed and jumptable is created.
/// This is very benefitial for testing and speeds up execution of that bytecode if called multiple times.
///
/// Default: Analyse
pub perf_analyse_created_bytecodes: AnalysisKind,
/// If some it will effects EIP-170: Contract code size limit. Usefull to increase this because of tests.
/// By default it is 0x6000 (~25kb).
pub limit_contract_code_size: Option<usize>,
/// 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,
/// Skip balance checks if true. Adds transaction cost to balance to ensure execution doesn't fail.
#[cfg(feature = "optional_balance_check")]
pub disable_balance_check: bool,
/// There are use cases where it's allowed to provide a gas limit that's higher than a block's gas limit. To that
/// end, you can disable the block gas limit validation.
/// By default, it is set to `false`.
#[cfg(feature = "optional_block_gas_limit")]
pub disable_block_gas_limit: bool,
/// EIP-3607 rejects transactions from senders with deployed code. In development, it can be desirable to simulate
/// calls from contracts, which this setting allows.
/// By default, it is set to `false`.
#[cfg(feature = "optional_eip3607")]
pub disable_eip3607: bool,
/// Disables all gas refunds. This is useful when using chains that have gas refunds disabled e.g. Avalanche.
/// Reasoning behind removing gas refunds can be found in EIP-3298.
/// By default, it is set to `false`.
#[cfg(feature = "optional_gas_refund")]
pub disable_gas_refund: bool,
/// Disables base fee checks for EIP-1559 transactions.
/// This is useful for testing method calls with zero gas price.
#[cfg(feature = "optional_no_base_fee")]
pub disable_base_fee: bool,
}
impl CfgEnv {
#[cfg(feature = "optional_eip3607")]
pub fn is_eip3607_disabled(&self) -> bool {
self.disable_eip3607
}
#[cfg(not(feature = "optional_eip3607"))]
pub fn is_eip3607_disabled(&self) -> bool {
false
}
#[cfg(feature = "optional_balance_check")]
pub fn is_balance_check_disabled(&self) -> bool {
self.disable_balance_check
}
#[cfg(not(feature = "optional_balance_check"))]
pub fn is_balance_check_disabled(&self) -> bool {
false
}
#[cfg(feature = "optional_gas_refund")]
pub fn is_gas_refund_disabled(&self) -> bool {
self.disable_gas_refund
}
#[cfg(not(feature = "optional_gas_refund"))]
pub fn is_gas_refund_disabled(&self) -> bool {
false
}
#[cfg(feature = "optional_no_base_fee")]
pub fn is_base_fee_check_disabled(&self) -> bool {
self.disable_base_fee
}
#[cfg(not(feature = "optional_no_base_fee"))]
pub fn is_base_fee_check_disabled(&self) -> bool {
false
}
#[cfg(feature = "optional_block_gas_limit")]
pub fn is_block_gas_limit_disabled(&self) -> bool {
self.disable_block_gas_limit
}
#[cfg(not(feature = "optional_block_gas_limit"))]
pub fn is_block_gas_limit_disabled(&self) -> bool {
false
}
}
#[derive(Clone, Default, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AnalysisKind {
Raw,
Check,
#[default]
Analyse,
}
impl Default for CfgEnv {
fn default() -> CfgEnv {
CfgEnv {
chain_id: U256::from(1),
spec_id: SpecId::LATEST,
perf_analyse_created_bytecodes: Default::default(),
limit_contract_code_size: None,
#[cfg(feature = "memory_limit")]
memory_limit: 2u64.pow(32) - 1,
#[cfg(feature = "optional_balance_check")]
disable_balance_check: false,
#[cfg(feature = "optional_block_gas_limit")]
disable_block_gas_limit: false,
#[cfg(feature = "optional_eip3607")]
disable_eip3607: false,
#[cfg(feature = "optional_gas_refund")]
disable_gas_refund: false,
#[cfg(feature = "optional_no_base_fee")]
disable_base_fee: false,
}
}
}
impl Default for BlockEnv {
fn default() -> BlockEnv {
BlockEnv {
gas_limit: U256::MAX,
number: U256::ZERO,
coinbase: B160::zero(),
timestamp: U256::from(1),
difficulty: U256::ZERO,
prevrandao: Some(B256::zero()),
basefee: U256::ZERO,
}
}
}
impl Default for TxEnv {
fn default() -> TxEnv {
TxEnv {
caller: B160::zero(),
gas_limit: u64::MAX,
gas_price: U256::ZERO,
gas_priority_fee: None,
transact_to: TransactTo::Call(B160::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(),
)
}
}
/// Validate ENV data of the block.
///
/// It can be skip if you are sure that PREVRANDAO is set.
#[inline]
pub fn validate_block_env<SPEC: Spec, T>(&self) -> Result<(), EVMError<T>> {
// Prevrandao is required for merge
if SPEC::enabled(SpecId::MERGE) && self.block.prevrandao.is_none() {
return Err(EVMError::PrevrandaoNotSet);
}
Ok(())
}
/// Validate transaction data that is set inside ENV and return error if something is wrong.
///
/// Return inital spend gas (Gas needed to execute transaction).
#[inline]
pub fn validate_tx<SPEC: Spec>(&self) -> Result<(), InvalidTransaction> {
let gas_limit = self.tx.gas_limit;
let effective_gas_price = self.effective_gas_price();
let is_create = self.tx.transact_to.is_create();
// BASEFEE tx check
if SPEC::enabled(SpecId::LONDON) {
if let Some(priority_fee) = self.tx.gas_priority_fee {
if priority_fee > self.tx.gas_price {
// or gas_max_fee for eip1559
return Err(InvalidTransaction::GasMaxFeeGreaterThanPriorityFee);
}
}
let basefee = self.block.basefee;
// check minimal cost against basefee
if !self.cfg.is_base_fee_check_disabled() && effective_gas_price < basefee {
return Err(InvalidTransaction::GasPriceLessThanBasefee);
}
}
// Check if gas_limit is more than block_gas_limit
if !self.cfg.is_block_gas_limit_disabled() && U256::from(gas_limit) > self.block.gas_limit {
return Err(InvalidTransaction::CallerGasLimitMoreThanBlock);
}
// EIP-3860: Limit and meter initcode
if SPEC::enabled(SpecId::SHANGHAI) && is_create && self.tx.data.len() > MAX_INITCODE_SIZE {
return Err(InvalidTransaction::CreateInitcodeSizeLimit);
}
// Check if the transaction's chain id is correct
if let Some(tx_chain_id) = self.tx.chain_id {
if U256::from(tx_chain_id) != self.cfg.chain_id {
return Err(InvalidTransaction::InvalidChainId);
}
}
// Check if the transaction's chain id is correct
if !SPEC::enabled(SpecId::BERLIN) && !self.tx.access_list.is_empty() {
return Err(InvalidTransaction::AccessListNotSupported);
}
Ok(())
}
/// Validate transaction agains state.
#[inline]
pub fn validate_tx_agains_state(&self, account: &Account) -> Result<(), InvalidTransaction> {
// EIP-3607: Reject transactions from senders with deployed code
// This EIP is introduced after london but there was no collision in past
// so we can leave it enabled always
if !self.cfg.is_eip3607_disabled() && account.info.code_hash != KECCAK_EMPTY {
return Err(InvalidTransaction::RejectCallerWithCode);
}
// Check that the transaction's nonce is correct
if let Some(tx) = self.tx.nonce {
let state = account.info.nonce;
match tx.cmp(&state) {
Ordering::Greater => {
return Err(InvalidTransaction::NonceTooHigh { tx, state });
}
Ordering::Less => {
return Err(InvalidTransaction::NonceTooLow { tx, state });
}
_ => {}
}
}
let balance_check = U256::from(self.tx.gas_limit)
.checked_mul(self.tx.gas_price)
.and_then(|gas_cost| gas_cost.checked_add(self.tx.value))
.ok_or(InvalidTransaction::OverflowPaymentInTransaction)?;
// Check if account has enough balance for gas_limit*gas_price and value transfer.
// Transfer will be done inside `*_inner` functions.
if !self.cfg.is_balance_check_disabled() && balance_check > account.info.balance {
return Err(InvalidTransaction::LackOfFundForMaxFee {
fee: self.tx.gas_limit,
balance: account.info.balance,
});
}
Ok(())
}
}