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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
use crate::{
account::LevmAccount,
constants::*,
errors::{ContextResult, ExceptionalHalt, InternalError, TxValidationError, VMError},
gas_cost::{STANDARD_TOKEN_COST, floor_tokens_in_access_list, total_cost_floor_per_token},
hooks::hook::Hook,
utils::*,
vm::VM,
};
use ethrex_common::{
Address, H256, U256,
types::{Code, Fork},
};
pub const MAX_REFUND_QUOTIENT: u64 = 5;
pub struct DefaultHook;
impl Hook for DefaultHook {
/// ## Description
/// This method performs validations and returns an error if any of these fail.
/// It also makes pre-execution changes:
/// - It increases sender nonce
/// - It substracts up-front-cost from sender balance.
/// - It adds value to receiver balance.
/// - It calculates and adds intrinsic gas to the 'gas used' of callframe and environment.
/// See 'docs' for more information about validations.
fn prepare_execution(&mut self, vm: &mut VM<'_>) -> Result<(), VMError> {
// System calls (EELS `process_unchecked_system_transaction`) have no
// sender semantics: no validation, no nonce bump, no fee deduction.
// EELS never reads the SYSTEM_ADDRESS account, so skip the whole
// sender path to keep the read out of execution witnesses (EIP-8025).
if vm.env.is_system_call {
let intrinsic = vm.get_intrinsic_gas()?;
vm.add_intrinsic_gas(&intrinsic)?;
transfer_value(vm)?;
set_bytecode_and_code_address(vm)?;
return Ok(());
}
let sender_address = vm.env.origin;
let sender_info = vm.db.get_account(sender_address)?.info.clone();
// Compute intrinsic gas once and reuse it for both the min-gas-limit
// validation and `add_intrinsic_gas` below (nothing in between mutates the
// calldata / access-list / auth-list it depends on).
let intrinsic = vm.get_intrinsic_gas()?;
if vm.env.config.fork >= Fork::Prague {
validate_min_gas_limit(vm, &intrinsic)?;
// EIP-7825 (Osaka to pre-Amsterdam): reject tx if gas_limit > POST_OSAKA_GAS_LIMIT_CAP.
// Amsterdam removes this restriction (EIP-8037 reservoir model).
if vm.env.config.fork >= Fork::Osaka
&& vm.env.config.fork < Fork::Amsterdam
&& vm.tx.gas_limit() > POST_OSAKA_GAS_LIMIT_CAP
{
return Err(VMError::TxValidation(
TxValidationError::TxMaxGasLimitExceeded {
tx_hash: vm.tx.hash(),
tx_gas_limit: vm.tx.gas_limit(),
},
));
}
}
// (1) GASLIMIT_PRICE_PRODUCT_OVERFLOW
let gaslimit_price_product = vm
.env
.gas_price
.checked_mul(vm.env.gas_limit.into())
.ok_or(TxValidationError::GasLimitPriceProductOverflow)?;
validate_sender_balance(vm, sender_info.balance)?;
// (2) INSUFFICIENT_MAX_FEE_PER_BLOB_GAS
if let Some(tx_max_fee_per_blob_gas) = vm.env.tx_max_fee_per_blob_gas {
validate_max_fee_per_blob_gas(vm, tx_max_fee_per_blob_gas)?;
}
// (3) INSUFFICIENT_ACCOUNT_FUNDS
deduct_caller(vm, gaslimit_price_product, sender_address)?;
// (4) INSUFFICIENT_MAX_FEE_PER_GAS
validate_sufficient_max_fee_per_gas(vm)?;
// (5) INITCODE_SIZE_EXCEEDED
if vm.is_create()? {
validate_init_code_size(vm)?;
}
// (6) INTRINSIC_GAS_TOO_LOW
vm.add_intrinsic_gas(&intrinsic)?;
// (7) NONCE_IS_MAX
vm.increment_account_nonce(sender_address)
.map_err(|_| TxValidationError::NonceIsMax)?;
// check for nonce mismatch
if sender_info.nonce != vm.env.tx_nonce {
return Err(TxValidationError::NonceMismatch {
expected: sender_info.nonce,
actual: vm.env.tx_nonce,
}
.into());
}
// (8) PRIORITY_GREATER_THAN_MAX_FEE_PER_GAS
if let (Some(tx_max_priority_fee), Some(tx_max_fee_per_gas)) = (
vm.env.tx_max_priority_fee_per_gas,
vm.env.tx_max_fee_per_gas,
) && tx_max_priority_fee > tx_max_fee_per_gas
{
return Err(TxValidationError::PriorityGreaterThanMaxFeePerGas {
priority_fee: tx_max_priority_fee,
max_fee_per_gas: tx_max_fee_per_gas,
}
.into());
}
// (9) SENDER_NOT_EOA
let code = vm.db.get_code(sender_info.code_hash)?;
validate_sender(sender_address, code.code())?;
// (10) GAS_ALLOWANCE_EXCEEDED
validate_gas_allowance(vm)?;
// Transaction is type 3 if tx_max_fee_per_blob_gas is Some
if vm.env.tx_max_fee_per_blob_gas.is_some() {
validate_4844_tx(vm)?;
}
// [EIP-7702]: https://eips.ethereum.org/EIPS/eip-7702
// Transaction is type 4 if authorization_list is Some
if vm.tx.authorization_list().is_some() {
validate_type_4_tx(vm)?;
}
transfer_value(vm)?;
set_bytecode_and_code_address(vm)?;
Ok(())
}
/// ## Changes post execution
/// 1. Undo value transfer if the transaction was reverted
/// 2. Return unused gas + gas refunds to the sender.
/// 3. Pay coinbase fee
/// 4. Destruct addresses in selfdestruct set.
fn finalize_execution(
&mut self,
vm: &mut VM<'_>,
ctx_result: &mut ContextResult,
) -> Result<(), VMError> {
// System calls (EELS `process_unchecked_system_transaction`) have no
// sender or fee semantics: there is nothing to undo, refund, or pay
// (the value and gas price are zero), so skip straight to the
// self-destruct cleanup. Callers ignore the gas accounting fields for
// system calls.
if vm.env.is_system_call {
delete_self_destruct_accounts(vm)?;
return Ok(());
}
if !ctx_result.is_success() {
undo_value_transfer(vm)?;
}
// EIP-8037 (Amsterdam+): CREATE-tx address collision.
// Per EELS process_message_call (interpreter.py:120-145) the collision
// returns `state_gas_left = message.state_gas_reservoir` (reservoir is
// PRESERVED, not burned). The failure block in fork.py:1086-1094 then
// adds `new_account_refund` to both `state_gas_left` and `state_refund`,
// so the user gets back reservoir + new_account_refund. tx_state_gas
// collapses to 0, tx_regular_gas = max(intrinsic_regular + message.gas,
// calldata_floor). The user does NOT lose the whole gas_limit.
if vm.env.config.fork >= Fork::Amsterdam && ctx_result.is_collision() {
let gas_limit = vm.env.gas_limit;
// state_gas_used is already net (signed, inline refunds applied).
// state_refund carries the EIP-7702 auth refund and CREATE-failure intrinsic
// (added by vm.finalize_execution). Clamp at zero.
let state_refund_signed =
i64::try_from(vm.state_refund).map_err(|_| InternalError::Overflow)?;
let state_gas: u64 =
u64::try_from(vm.state_gas_used.saturating_sub(state_refund_signed).max(0))
.map_err(|_| InternalError::Overflow)?;
let floor = vm.get_min_gas_used()?;
// Regular gas = gas_limit - state_gas_left, where state_gas_left =
// reservoir (PRESERVED across collision in EELS, with new_account_refund
// already folded in by vm.finalize_execution above). Mirrors EELS
// tx_gas_used_before_refund = tx.gas - gas_left(=0) - state_gas_left.
let regular_gas = gas_limit.saturating_sub(vm.state_gas_reservoir);
let effective_regular = regular_gas.max(floor);
ctx_result.gas_used = effective_regular
.checked_add(state_gas)
.ok_or(InternalError::Overflow)?;
// User pays only the effective regular (post-floor); coinbase gets the
// same; remainder returns to sender.
ctx_result.gas_spent = effective_regular;
pay_coinbase(vm, effective_regular)?;
let gas_to_return = gas_limit
.checked_sub(effective_regular)
.ok_or(InternalError::Underflow)?;
let wei_return_amount = vm
.env
.gas_price
.checked_mul(U256::from(gas_to_return))
.ok_or(InternalError::Overflow)?;
vm.increase_account_balance(vm.env.origin, wei_return_amount)?;
return Ok(());
}
// EIP-8037 (Amsterdam+): unused reservoir is always returned to sender.
// Per EELS, state_gas_left is preserved even on exceptional halt — only
// regular gas_left is burned. The user does NOT pay for unspent reservoir.
if vm.env.config.fork >= Fork::Amsterdam {
ctx_result.gas_used = ctx_result.gas_used.saturating_sub(vm.state_gas_reservoir);
}
// Save pre-refund gas for EIP-7778 block accounting
let gas_used_pre_refund = ctx_result.gas_used;
// Note: compute_gas_refunded caps at gas_used / MAX_REFUND_QUOTIENT, where
// gas_used already has the reservoir subtracted (line above). This matches
// EELS, which applies the refund cap after reservoir removal but before the
// regular/state gas split.
let gas_refunded: u64 = compute_gas_refunded(vm, ctx_result)?;
let gas_spent = compute_actual_gas_used(vm, gas_refunded, gas_used_pre_refund)?;
refund_sender(vm, ctx_result, gas_refunded, gas_spent)?;
pay_coinbase(vm, gas_spent)?;
delete_self_destruct_accounts(vm)?;
Ok(())
}
}
pub fn undo_value_transfer(vm: &mut VM<'_>) -> Result<(), VMError> {
// In a create if Tx was reverted the account won't even exist by this point.
if !vm.is_create()? {
vm.decrease_account_balance(vm.current_call_frame.to, vm.current_call_frame.msg_value)?;
}
vm.increase_account_balance(vm.env.origin, vm.current_call_frame.msg_value)?;
Ok(())
}
/// Refunds unused gas to the sender. The user pays `gas_spent` (post-refund);
/// for Amsterdam+, block-level accounting is recomputed dimensionally from VM
/// fields, not from a pre-refund total.
pub fn refund_sender(
vm: &mut VM<'_>,
ctx_result: &mut ContextResult,
refunded_gas: u64,
gas_spent: u64,
) -> Result<(), VMError> {
vm.substate.refunded_gas = refunded_gas;
// EIP-7778: Separate block vs user gas accounting for Amsterdam+
// Block header gas_used = max(regular_dimension, state_dimension) per EIP-7778.
// Receipt cumulative_gas_used = post-refund total (what user pays).
if vm.env.config.fork >= Fork::Amsterdam {
// EIP-7623 floor applies to the regular (non-state) gas component only.
let floor = vm.get_min_gas_used()?;
// EIP-8037: state_gas_used is already net (signed, credits applied inline).
// Subtract state_refund (EIP-7702 tx-level channel) and clamp at zero.
let state_refund_signed =
i64::try_from(vm.state_refund).map_err(|_| InternalError::Overflow)?;
let state_gas: u64 =
u64::try_from(vm.state_gas_used.saturating_sub(state_refund_signed).max(0))
.map_err(|_| InternalError::Overflow)?;
// Compute raw consumption from scratch (gas_limit minus gas_remaining)
// to avoid interference from any reservoir-current subtraction baked
// into the caller's pre-refund number.
#[expect(clippy::as_conversions, reason = "gas_remaining is >= 0 here")]
let gas_remaining = vm.current_call_frame.gas_remaining.max(0) as u64;
let raw_consumed = vm.env.gas_limit.saturating_sub(gas_remaining);
// Subtract intrinsic_state (pre-consumed from gas_remaining as part of total intrinsic),
// the initial reservoir (pre-consumed from gas_remaining), and state-gas spills
// (EELS charge_state_gas spills don't count as regular_gas_used).
let regular_gas = raw_consumed
.saturating_sub(vm.intrinsic_state_gas)
.saturating_sub(vm.state_gas_reservoir_initial)
.saturating_sub(vm.state_gas_spill);
let effective_regular = regular_gas.max(floor);
ctx_result.gas_used = effective_regular
.checked_add(state_gas)
.ok_or(InternalError::Overflow)?;
// User pays post-refund gas (with floor)
ctx_result.gas_spent = gas_spent;
} else {
// Pre-Amsterdam: both use post-refund value
ctx_result.gas_used = gas_spent;
ctx_result.gas_spent = gas_spent;
}
// Return unspent gas to the sender (based on what user pays)
let gas_to_return = vm
.env
.gas_limit
.checked_sub(gas_spent)
.ok_or(InternalError::Underflow)?;
let wei_return_amount = vm
.env
.gas_price
.checked_mul(U256::from(gas_to_return))
.ok_or(InternalError::Overflow)?;
vm.increase_account_balance(vm.env.origin, wei_return_amount)?;
Ok(())
}
// [EIP-3529](https://eips.ethereum.org/EIPS/eip-3529)
pub fn compute_gas_refunded(vm: &VM<'_>, ctx_result: &ContextResult) -> Result<u64, VMError> {
Ok(vm
.substate
.refunded_gas
.min(ctx_result.gas_used / MAX_REFUND_QUOTIENT))
}
// Calculate actual gas used in the whole transaction. Since Prague there is a base minimum to be consumed.
pub fn compute_actual_gas_used(
vm: &mut VM<'_>,
refunded_gas: u64,
gas_used_without_refunds: u64,
) -> Result<u64, VMError> {
let exec_gas_consumed = gas_used_without_refunds
.checked_sub(refunded_gas)
.ok_or(InternalError::Underflow)?;
if vm.env.config.fork >= Fork::Prague {
Ok(exec_gas_consumed.max(vm.get_min_gas_used()?))
} else {
Ok(exec_gas_consumed)
}
}
pub fn pay_coinbase(vm: &mut VM<'_>, gas_to_pay: u64) -> Result<(), VMError> {
let priority_fee_per_gas = vm
.env
.gas_price
.checked_sub(vm.env.base_fee_per_gas)
.ok_or(InternalError::Underflow)?;
let coinbase_fee = U256::from(gas_to_pay)
.checked_mul(priority_fee_per_gas)
.ok_or(InternalError::Overflow)?;
// Per EIP-7928: Coinbase must appear in BAL when there's a user transaction,
// even if the priority fee is zero. System contract calls have gas_price = 0,
// so we use this to distinguish them from user transactions.
if !vm.env.gas_price.is_zero()
&& let Some(recorder) = vm.db.bal_recorder.as_mut()
{
recorder.record_touched_address(vm.env.coinbase);
}
// Only pay coinbase if there's actually a fee to pay.
if !coinbase_fee.is_zero() {
vm.increase_account_balance(vm.env.coinbase, coinbase_fee)?;
} else if !vm.env.is_system_call {
// The spec reads the coinbase account unconditionally during user-tx
// fee transfer (EELS `process_transaction` calls `get_account` before
// deciding whether to credit), but system contract calls never touch
// the coinbase. Keep the read observable for zero-fee user txs
// (including gas-price-zero txs on zero-base-fee chains) so execution
// witnesses (EIP-8025) record the coinbase trie path, including its
// exclusion proof when it doesn't exist.
vm.db.get_account(vm.env.coinbase)?;
}
Ok(())
}
// In Cancun the only addresses destroyed are contracts created in this transaction
pub fn delete_self_destruct_accounts(vm: &mut VM<'_>) -> Result<(), VMError> {
// EIP-7708: Emit Burn logs for accounts with non-zero balance marked for deletion
// Must emit in lexicographical order of address
if vm.env.config.fork >= Fork::Amsterdam {
let mut addresses_with_balance: Vec<(Address, U256)> = vm
.substate
.iter_selfdestruct()
.filter_map(|addr| {
let balance = vm.db.get_account(*addr).ok()?.info.balance;
if !balance.is_zero() {
Some((*addr, balance))
} else {
None
}
})
.collect();
// Sort by address (lexicographical order per EIP-7708)
addresses_with_balance.sort_by_key(|(addr, _)| *addr);
for (addr, balance) in addresses_with_balance {
let log = create_burn_log(addr, balance);
vm.substate.add_log(log);
}
}
// Delete the accounts
for address in vm.substate.iter_selfdestruct() {
// Backup must be taken before mark_modified flips `exists` to true.
let account_to_remove = vm.db.get_account(*address)?;
vm.current_call_frame
.call_frame_backup
.backup_account_info(*address, account_to_remove)?;
let account_to_remove = vm.db.get_account_mut(*address)?;
*account_to_remove = LevmAccount::default();
account_to_remove.mark_destroyed();
// EIP-7928: Clean up BAL for selfdestructed account
if let Some(recorder) = vm.db.bal_recorder.as_mut() {
recorder.track_selfdestruct(*address);
}
}
Ok(())
}
pub fn validate_min_gas_limit(vm: &mut VM<'_>, intrinsic: &IntrinsicGas) -> Result<(), VMError> {
// check for gas limit is grater or equal than the minimum required
let regular_gas = intrinsic.regular;
let state_gas = intrinsic.state;
let intrinsic_gas: u64 = regular_gas
.checked_add(state_gas)
.ok_or(ExceptionalHalt::OutOfGas)?;
if vm.current_call_frame.gas_limit < intrinsic_gas {
return Err(TxValidationError::IntrinsicGasTooLow.into());
}
let fork = vm.env.config.fork;
// EIP-7976 floor tokens: for the floor arm, all calldata bytes count unweighted.
// floor_tokens_in_calldata = (zero_bytes + nonzero_bytes) * STANDARD_TOKEN_COST
// Pre-Amsterdam uses the weighted EIP-7623 formula: (nonzero * 16 + zero * 4) / 4
let mut tokens_in_calldata: u64 = if fork >= Fork::Amsterdam {
// EIP-7976: floor tokens = total_bytes * STANDARD_TOKEN_COST (unweighted).
let total_bytes: u64 = vm
.current_call_frame
.calldata
.len()
.try_into()
.map_err(|_| InternalError::TypeConversion)?;
total_bytes
.checked_mul(STANDARD_TOKEN_COST)
.ok_or(InternalError::Overflow)?
} else {
// Pre-Amsterdam: weighted EIP-7623 token count. Reuse the calldata cost already
// computed in `intrinsic` (same byte string) instead of re-walking the calldata.
intrinsic.calldata_cost / STANDARD_TOKEN_COST
};
// EIP-7981 (Amsterdam+): access-list data bytes fold into the floor-token count.
// floor_tokens_in_access_list = access_list_bytes * STANDARD_TOKEN_COST
// where access_list_bytes = 20 * address_count + 32 * storage_key_count.
if fork >= Fork::Amsterdam {
let al_floor_tokens = floor_tokens_in_access_list(vm.tx.access_list());
tokens_in_calldata = tokens_in_calldata
.checked_add(al_floor_tokens)
.ok_or(InternalError::Overflow)?;
}
// floor_cost_by_tokens = TX_BASE_COST + total_cost_floor_per_token(fork) * tokens
// EIP-7976 (Amsterdam+) raises the floor multiplier from 10 to 16.
let floor_cost_by_tokens = tokens_in_calldata
.checked_mul(total_cost_floor_per_token(fork))
.ok_or(InternalError::Overflow)?
.checked_add(TX_BASE_COST)
.ok_or(InternalError::Overflow)?;
// EIP-8037 (Amsterdam+): Regular gas is capped at TX_MAX_GAS_LIMIT — reject if
// intrinsic regular gas or calldata floor exceeds the cap (no amount of gas_limit
// can make the TX valid since excess gas_limit becomes state gas reservoir).
// Must be checked before the floor check so the correct error is returned.
// NOTE: We use IntrinsicGasTooLow (not TxMaxGasLimitExceeded) intentionally —
// this matches the EELS exception mapping for this specific case.
if vm.env.config.fork >= Fork::Amsterdam
&& regular_gas.max(floor_cost_by_tokens) > TX_MAX_GAS_LIMIT_AMSTERDAM
{
return Err(TxValidationError::IntrinsicGasTooLow.into());
}
if vm.current_call_frame.gas_limit < floor_cost_by_tokens {
return Err(TxValidationError::IntrinsicGasBelowFloorGasCost.into());
}
Ok(())
}
pub fn validate_max_fee_per_blob_gas(
vm: &mut VM<'_>,
tx_max_fee_per_blob_gas: U256,
) -> Result<(), VMError> {
let base_fee_per_blob_gas = vm.env.base_blob_fee_per_gas;
if tx_max_fee_per_blob_gas < base_fee_per_blob_gas {
return Err(TxValidationError::InsufficientMaxFeePerBlobGas {
base_fee_per_blob_gas,
tx_max_fee_per_blob_gas,
}
.into());
}
Ok(())
}
pub fn validate_init_code_size(vm: &mut VM<'_>) -> Result<(), VMError> {
// [EIP-3860] - INITCODE_SIZE_EXCEEDED
// [EIP-7954] - Amsterdam increases the limit
let code_size = vm.current_call_frame.calldata.len();
let max_size = if vm.env.config.fork >= Fork::Amsterdam {
AMSTERDAM_INIT_CODE_MAX_SIZE
} else {
INIT_CODE_MAX_SIZE
};
if code_size > max_size && vm.env.config.fork >= Fork::Shanghai {
return Err(TxValidationError::InitcodeSizeExceeded {
max_size,
actual_size: code_size,
}
.into());
}
Ok(())
}
pub fn validate_sufficient_max_fee_per_gas(vm: &mut VM<'_>) -> Result<(), TxValidationError> {
if vm.env.tx_max_fee_per_gas.unwrap_or(vm.env.gas_price) < vm.env.base_fee_per_gas {
return Err(TxValidationError::InsufficientMaxFeePerGas);
}
Ok(())
}
pub fn validate_4844_tx(vm: &mut VM<'_>) -> Result<(), VMError> {
// (11) TYPE_3_TX_PRE_FORK
if vm.env.config.fork < Fork::Cancun {
return Err(TxValidationError::Type3TxPreFork.into());
}
let blob_hashes = &vm.env.tx_blob_hashes;
// (12) TYPE_3_TX_ZERO_BLOBS
if blob_hashes.is_empty() {
return Err(TxValidationError::Type3TxZeroBlobs.into());
}
// (13) TYPE_3_TX_INVALID_BLOB_VERSIONED_HASH
for blob_hash in blob_hashes {
let blob_hash = blob_hash.as_bytes();
if blob_hash
.first()
.is_some_and(|first_byte| !VALID_BLOB_PREFIXES.contains(first_byte))
{
return Err(TxValidationError::Type3TxInvalidBlobVersionedHash.into());
}
}
// (14) TYPE_3_TX_BLOB_COUNT_EXCEEDED
let max_blob_count = vm
.env
.config
.blob_schedule
.max
.try_into()
.map_err(|_| InternalError::TypeConversion)?;
let blob_count = blob_hashes.len();
if blob_count > max_blob_count {
return Err(TxValidationError::Type3TxBlobCountExceeded {
max_blob_count,
actual_blob_count: blob_count,
}
.into());
}
if vm.env.config.fork >= Fork::Osaka && blob_count > MAX_BLOB_COUNT_TX {
return Err(TxValidationError::Type3TxBlobCountExceeded {
max_blob_count: MAX_BLOB_COUNT_TX,
actual_blob_count: blob_count,
}
.into());
}
// (15) TYPE_3_TX_CONTRACT_CREATION
// NOTE: This will never happen, since the EIP-4844 tx (type 3) does not have a TxKind field
// only supports an Address which must be non-empty.
// If a type 3 tx has the field `to` as null (signaling create), it will raise an exception on RLP decoding,
// it won't reach this point.
// For more information, please check the following thread:
// - https://github.com/lambdaclass/ethrex/pull/2425/files/819825516dc633275df56b2886b921061c4d7681#r2035611105
if vm.is_create()? {
return Err(TxValidationError::Type3TxContractCreation.into());
}
Ok(())
}
pub fn validate_type_4_tx(vm: &mut VM<'_>) -> Result<(), VMError> {
let Some(auth_list) = vm.tx.authorization_list() else {
// vm.authorization_list should be Some at this point.
return Err(InternalError::Custom("Auth list not found".to_string()).into());
};
// (16) TYPE_4_TX_PRE_FORK
if vm.env.config.fork < Fork::Prague {
return Err(TxValidationError::Type4TxPreFork.into());
}
// (17) TYPE_4_TX_CONTRACT_CREATION
// From the EIP docs: a null destination is not valid.
// NOTE: This will never happen, since the EIP-7702 tx (type 4) does not have a TxKind field
// only supports an Address which must be non-empty.
// If a type 4 tx has the field `to` as null (signaling create), it will raise an exception on RLP decoding,
// it won't reach this point.
// For more information, please check the following thread:
// - https://github.com/lambdaclass/ethrex/pull/2425/files/819825516dc633275df56b2886b921061c4d7681#r2035611105
if vm.is_create()? {
return Err(TxValidationError::Type4TxContractCreation.into());
}
// (18) TYPE_4_TX_LIST_EMPTY
// From the EIP docs: The transaction is considered invalid if the length of authorization_list is zero.
if auth_list.is_empty() {
return Err(TxValidationError::Type4TxAuthorizationListIsEmpty.into());
}
vm.eip7702_set_access_code()
}
pub fn validate_sender(sender_address: Address, code: &[u8]) -> Result<(), VMError> {
if !code.is_empty() && !code_has_delegation(code)? {
return Err(TxValidationError::SenderNotEOA(sender_address).into());
}
Ok(())
}
pub fn validate_gas_allowance(vm: &mut VM<'_>) -> Result<(), TxValidationError> {
// System contract calls (EIP-2935, EIP-4788, EIP-7002, EIP-7251) bypass the
// block-level gas-allowance check — their 30M gas budget is a protocol rule
// independent of `block_gas_limit`.
if vm.env.is_system_call {
return Ok(());
}
if vm.env.gas_limit > vm.env.block_gas_limit {
return Err(TxValidationError::GasAllowanceExceeded {
block_gas_limit: vm.env.block_gas_limit,
tx_gas_limit: vm.env.gas_limit,
});
}
Ok(())
}
pub fn validate_sender_balance(vm: &mut VM<'_>, sender_balance: U256) -> Result<(), VMError> {
if vm.env.disable_balance_check {
return Ok(());
}
// Up front cost is the maximum amount of wei that a user is willing to pay for. Gaslimit * gasprice + value + blob_gas_cost
let value = vm.current_call_frame.msg_value;
// blob gas cost = max fee per blob gas * blob gas used
// https://eips.ethereum.org/EIPS/eip-4844
let max_blob_gas_cost =
get_max_blob_gas_price(&vm.env.tx_blob_hashes, vm.env.tx_max_fee_per_blob_gas)?;
// For the transaction to be valid the sender account has to have a balance >= gas_price * gas_limit + value if tx is type 0 and 1
// balance >= max_fee_per_gas * gas_limit + value + blob_gas_cost if tx is type 2 or 3
let gas_fee_for_valid_tx = vm
.env
.tx_max_fee_per_gas
.unwrap_or(vm.env.gas_price)
.checked_mul(vm.env.gas_limit.into())
.ok_or(TxValidationError::GasLimitPriceProductOverflow)?;
let balance_for_valid_tx = gas_fee_for_valid_tx
.checked_add(value)
.ok_or(TxValidationError::InsufficientAccountFunds)?
.checked_add(max_blob_gas_cost)
.ok_or(TxValidationError::InsufficientAccountFunds)?;
if sender_balance < balance_for_valid_tx {
return Err(TxValidationError::InsufficientAccountFunds.into());
}
Ok(())
}
pub fn deduct_caller(
vm: &mut VM<'_>,
gas_limit_price_product: U256,
sender_address: Address,
) -> Result<(), VMError> {
if vm.env.disable_balance_check {
return Ok(());
}
// Up front cost is the maximum amount of wei that a user is willing to pay for. Gaslimit * gasprice + value + blob_gas_cost
let value = vm.current_call_frame.msg_value;
let blob_gas_cost =
calculate_blob_gas_cost(&vm.env.tx_blob_hashes, vm.env.base_blob_fee_per_gas)?;
// The real cost to deduct is calculated as effective_gas_price * gas_limit + value + blob_gas_cost
let up_front_cost = gas_limit_price_product
.checked_add(value)
.ok_or(TxValidationError::InsufficientAccountFunds)?
.checked_add(blob_gas_cost)
.ok_or(TxValidationError::InsufficientAccountFunds)?;
// There is no error specified for overflow in up_front_cost
// in ef_tests. We went for "InsufficientAccountFunds" simply
// because if the upfront cost is bigger than U256, then,
// technically, the sender will not be able to pay it.
vm.decrease_account_balance(sender_address, up_front_cost)
.map_err(|_| TxValidationError::InsufficientAccountFunds)?;
Ok(())
}
/// Transfer msg_value to transaction recipient
pub fn transfer_value(vm: &mut VM<'_>) -> Result<(), VMError> {
if !vm.is_create()? {
let value = vm.current_call_frame.msg_value;
let to = vm.current_call_frame.to;
vm.increase_account_balance(to, value)?;
// EIP-7708: Emit transfer log for nonzero-value transactions to DIFFERENT accounts
// Self-transfers (origin == to) should NOT emit a log per the EIP spec
let from = vm.env.origin;
if vm.env.config.fork >= Fork::Amsterdam && !value.is_zero() && from != to {
let log = create_eth_transfer_log(from, to, value);
vm.substate.add_log(log);
}
}
Ok(())
}
/// Sets bytecode and code_address to CallFrame
pub fn set_bytecode_and_code_address(vm: &mut VM<'_>) -> Result<(), VMError> {
// Get bytecode and code_address for assigning those values to the callframe.
let (bytecode, code_address) = if vm.is_create()? {
// Here bytecode is the calldata and the code_address is just the created contract address.
let calldata = std::mem::take(&mut vm.current_call_frame.calldata);
(
// SAFETY: we don't need the hash for the initcode
Code::from_bytecode_unchecked(calldata, H256::zero()),
vm.current_call_frame.to,
)
} else {
// Here bytecode and code_address could be either from the account or from the delegated account.
let to = vm.current_call_frame.to;
// Record tx.to as touched in BAL (the target of message call transaction)
if let Some(recorder) = vm.db.bal_recorder.as_mut() {
recorder.record_touched_address(to);
}
let (is_delegation, _eip7702_gas_consumed, code_address, bytecode) =
eip7702_get_code(vm.db, &mut vm.substate, to)?;
// If EIP-7702 delegation, also record the delegation target (code source) in BAL
if is_delegation && let Some(recorder) = vm.db.bal_recorder.as_mut() {
recorder.record_touched_address(code_address);
}
(bytecode, code_address)
};
// Assign code and code_address to callframe
vm.current_call_frame.code_address = code_address;
vm.current_call_frame.set_code(bytecode)?;
Ok(())
}