Skip to main content

revm_handler/
pre_execution.rs

1//! Handles related to the main function of the EVM.
2//!
3//! They handle initial setup of the EVM, call loop and the final return of the EVM
4
5use crate::{EvmTr, PrecompileProvider};
6use bytecode::Bytecode;
7use context_interface::{
8    journaled_state::{account::JournaledAccountTr, JournalTr},
9    result::InvalidTransaction,
10    transaction::{AccessListItemTr, AuthorizationTr, Transaction, TransactionType},
11    Block, Cfg, ContextTr, Database,
12};
13use core::cmp::Ordering;
14use primitives::{hardfork::SpecId, AddressMap, HashSet, StorageKey, U256};
15use state::AccountInfo;
16
17/// Loads and warms accounts for execution, including precompiles and access list.
18pub fn load_accounts<
19    EVM: EvmTr<Precompiles: PrecompileProvider<EVM::Context>>,
20    ERROR: From<<<EVM::Context as ContextTr>::Db as Database>::Error>,
21>(
22    evm: &mut EVM,
23) -> Result<(), ERROR> {
24    let (context, precompiles) = evm.ctx_precompiles();
25
26    let gen_spec = context.cfg().spec();
27    let spec = gen_spec.clone().into();
28    // sets eth spec id in journal
29    context.journal_mut().set_spec_id(spec);
30    let precompiles_changed = precompiles.set_spec(gen_spec);
31    let empty_warmed_precompiles = context.journal_mut().precompile_addresses().is_empty();
32
33    if precompiles_changed || empty_warmed_precompiles {
34        // load new precompile addresses into journal.
35        // When precompiles addresses are changed we reset the warmed hashmap to those new addresses.
36        context
37            .journal_mut()
38            .warm_precompiles(precompiles.warm_addresses().collect());
39    }
40
41    // Load coinbase
42    // EIP-3651: Warm COINBASE. Starts the `COINBASE` address warm
43    if spec.is_enabled_in(SpecId::SHANGHAI) {
44        let coinbase = context.block().beneficiary();
45        context.journal_mut().warm_coinbase_account(coinbase);
46    }
47
48    // Load access list
49    let (tx, journal) = context.tx_journal_mut();
50    // legacy is only tx type that does not have access list.
51    if tx.tx_type() != TransactionType::Legacy {
52        if let Some(access_list) = tx.access_list() {
53            let mut map: AddressMap<HashSet<StorageKey>> = AddressMap::default();
54            for item in access_list {
55                map.entry(*item.address())
56                    .or_default()
57                    .extend(item.storage_slots().map(|key| U256::from_be_bytes(key.0)));
58            }
59            journal.warm_access_list(map);
60        }
61    }
62
63    Ok(())
64}
65
66/// Validates caller account nonce and code according to EIP-3607.
67#[inline]
68pub fn validate_account_nonce_and_code_with_components(
69    caller_info: &AccountInfo,
70    tx: impl Transaction,
71    cfg: impl Cfg,
72) -> Result<(), InvalidTransaction> {
73    validate_account_nonce_and_code(
74        caller_info,
75        tx.nonce(),
76        cfg.is_eip3607_disabled(),
77        cfg.is_nonce_check_disabled(),
78    )
79}
80
81/// Validates caller account nonce and code according to EIP-3607.
82#[inline]
83pub fn validate_account_nonce_and_code(
84    caller_info: &AccountInfo,
85    tx_nonce: u64,
86    is_eip3607_disabled: bool,
87    is_nonce_check_disabled: bool,
88) -> Result<(), InvalidTransaction> {
89    // EIP-3607: Reject transactions from senders with deployed code
90    // This EIP is introduced after london but there was no collision in past
91    // so we can leave it enabled always
92    if !is_eip3607_disabled {
93        let bytecode = match caller_info.code.as_ref() {
94            Some(code) => code,
95            None => &Bytecode::default(),
96        };
97        // Allow EOAs whose code is a valid delegation designation,
98        // i.e. 0xef0100 || address, to continue to originate transactions.
99        if !bytecode.is_empty() && !bytecode.is_eip7702() {
100            return Err(InvalidTransaction::RejectCallerWithCode);
101        }
102    }
103
104    // Check that the transaction's nonce is correct
105    if !is_nonce_check_disabled {
106        let tx = tx_nonce;
107        let state = caller_info.nonce;
108        match tx.cmp(&state) {
109            Ordering::Greater => {
110                return Err(InvalidTransaction::NonceTooHigh { tx, state });
111            }
112            Ordering::Less => {
113                return Err(InvalidTransaction::NonceTooLow { tx, state });
114            }
115            _ => {}
116        }
117    }
118    Ok(())
119}
120
121/// Check maximum possible fee and deduct the effective fee.
122///
123/// Returns new balance.
124#[inline]
125pub fn calculate_caller_fee(
126    balance: U256,
127    tx: impl Transaction,
128    block: impl Block,
129    cfg: impl Cfg,
130) -> Result<U256, InvalidTransaction> {
131    // If fee charge is disabled, return the balance as-is without deducting fees.
132    // This is useful for `eth_call` and similar simulation scenarios.
133    if cfg.is_fee_charge_disabled() {
134        return Ok(balance);
135    }
136
137    let basefee = block.basefee() as u128;
138    let blob_price = block.blob_gasprice().unwrap_or_default();
139    let is_balance_check_disabled = cfg.is_balance_check_disabled();
140
141    if !is_balance_check_disabled {
142        tx.ensure_enough_balance(balance)?;
143    }
144
145    let effective_balance_spending = tx
146        .effective_balance_spending(basefee, blob_price)
147        .expect("effective balance is always smaller than max balance so it can't overflow");
148
149    let gas_balance_spending = effective_balance_spending - tx.value();
150
151    // new balance
152    let mut new_balance = balance.saturating_sub(gas_balance_spending);
153
154    if is_balance_check_disabled {
155        // Make sure the caller's balance is at least the value of the transaction.
156        new_balance = new_balance.max(tx.value());
157    }
158
159    Ok(new_balance)
160}
161
162/// Validates caller state and deducts transaction costs from the caller's balance.
163#[inline]
164pub fn validate_against_state_and_deduct_caller<
165    CTX: ContextTr,
166    ERROR: From<InvalidTransaction> + From<<CTX::Db as Database>::Error>,
167>(
168    context: &mut CTX,
169) -> Result<(), ERROR> {
170    let (block, tx, cfg, journal, _, _) = context.all_mut();
171
172    // Load caller's account.
173    let mut caller = journal.load_account_with_code_mut(tx.caller())?.data;
174
175    validate_account_nonce_and_code_with_components(&caller.account().info, tx, cfg)?;
176
177    let new_balance = calculate_caller_fee(*caller.balance(), tx, block, cfg)?;
178
179    caller.set_balance(new_balance);
180    if tx.kind().is_call() {
181        caller.bump_nonce();
182    }
183    Ok(())
184}
185
186/// Apply EIP-7702 auth list and return number gas refund on already created accounts.
187///
188/// Note that this function will do nothing if the transaction type is not EIP-7702.
189/// If you need to apply auth list for other transaction types, use [`apply_auth_list`] function.
190///
191/// Internally uses [`apply_auth_list`] function.
192#[inline]
193pub fn apply_eip7702_auth_list<
194    CTX: ContextTr,
195    ERROR: From<InvalidTransaction> + From<<CTX::Db as Database>::Error>,
196>(
197    context: &mut CTX,
198) -> Result<u64, ERROR> {
199    let chain_id = context.cfg().chain_id();
200    let refund_per_auth = context.cfg().gas_params().tx_eip7702_auth_refund();
201    let (tx, journal) = context.tx_journal_mut();
202
203    // Return if not EIP-7702 transaction.
204    if tx.tx_type() != TransactionType::Eip7702 {
205        return Ok(0);
206    }
207    apply_auth_list(chain_id, refund_per_auth, tx.authorization_list(), journal)
208}
209
210/// Apply EIP-7702 style auth list and return number gas refund on already created accounts.
211///
212/// It is more granular function from [`apply_eip7702_auth_list`] function as it takes only the list, journal and chain id.
213///
214/// The `refund_per_auth` parameter specifies the gas refund per existing account authorization.
215/// By default this is `PER_EMPTY_ACCOUNT_COST - PER_AUTH_BASE_COST` (25000 - 12500 = 12500),
216/// but can be configured via [`GasParams::tx_eip7702_auth_refund`](context_interface::cfg::gas_params::GasParams::tx_eip7702_auth_refund).
217#[inline]
218pub fn apply_auth_list<
219    JOURNAL: JournalTr,
220    ERROR: From<InvalidTransaction> + From<<JOURNAL::Database as Database>::Error>,
221>(
222    chain_id: u64,
223    refund_per_auth: u64,
224    auth_list: impl Iterator<Item = impl AuthorizationTr>,
225    journal: &mut JOURNAL,
226) -> Result<u64, ERROR> {
227    let mut refunded_accounts = 0;
228    for authorization in auth_list {
229        // 1. Verify the chain id is either 0 or the chain's current ID.
230        let auth_chain_id = authorization.chain_id();
231        if !auth_chain_id.is_zero() && auth_chain_id != U256::from(chain_id) {
232            continue;
233        }
234
235        // 2. Verify the `nonce` is less than `2**64 - 1`.
236        if authorization.nonce() == u64::MAX {
237            continue;
238        }
239
240        // recover authority and authorized addresses.
241        // 3. `authority = ecrecover(keccak(MAGIC || rlp([chain_id, address, nonce])), y_parity, r, s]`
242        let Some(authority) = authorization.authority() else {
243            continue;
244        };
245
246        // warm authority account and check nonce.
247        // 4. Add `authority` to `accessed_addresses` (as defined in [EIP-2929](./eip-2929.md).)
248        let mut authority_acc = journal.load_account_with_code_mut(authority)?;
249        let authority_acc_info = &authority_acc.account().info;
250
251        // 5. Verify the code of `authority` is either empty or already delegated.
252        if let Some(bytecode) = &authority_acc_info.code {
253            // if it is not empty and it is not eip7702
254            if !bytecode.is_empty() && !bytecode.is_eip7702() {
255                continue;
256            }
257        }
258
259        // 6. Verify the nonce of `authority` is equal to `nonce`. In case `authority` does not exist in the trie, verify that `nonce` is equal to `0`.
260        if authorization.nonce() != authority_acc_info.nonce {
261            continue;
262        }
263
264        // 7. Add `PER_EMPTY_ACCOUNT_COST - PER_AUTH_BASE_COST` gas to the global refund counter if `authority` exists in the trie.
265        if !(authority_acc_info.is_empty()
266            && authority_acc
267                .account()
268                .is_loaded_as_not_existing_not_touched())
269        {
270            refunded_accounts += 1;
271        }
272
273        // 8. Set the code of `authority` to be `0xef0100 || address`. This is a delegation designation.
274        //  * As a special case, if `address` is `0x0000000000000000000000000000000000000000` do not write the designation.
275        //    Clear the accounts code and reset the account's code hash to the empty hash `0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470`.
276        // 9. Increase the nonce of `authority` by one.
277        authority_acc.delegate(authorization.address());
278    }
279
280    let refunded_gas = refunded_accounts * refund_per_auth;
281
282    Ok(refunded_gas)
283}