revm_handler/
pre_execution.rs1use crate::{EvmTr, PrecompileProvider};
6use bytecode::Bytecode;
7use context_interface::transaction::{AccessListItemTr, AuthorizationTr};
8use context_interface::ContextTr;
9use context_interface::{
10 journaled_state::JournalTr,
11 result::InvalidTransaction,
12 transaction::{Transaction, TransactionType},
13 Block, Cfg, Database,
14};
15use core::cmp::Ordering;
16use primitives::StorageKey;
17use primitives::{eip7702, hardfork::SpecId, KECCAK_EMPTY, U256};
18use state::AccountInfo;
19
20pub fn load_accounts<
22 EVM: EvmTr<Precompiles: PrecompileProvider<EVM::Context>>,
23 ERROR: From<<<EVM::Context as ContextTr>::Db as Database>::Error>,
24>(
25 evm: &mut EVM,
26) -> Result<(), ERROR> {
27 let (context, precompiles) = evm.ctx_precompiles();
28
29 let gen_spec = context.cfg().spec();
30 let spec = gen_spec.clone().into();
31 context.journal_mut().set_spec_id(spec);
33 let precompiles_changed = precompiles.set_spec(gen_spec);
34 let empty_warmed_precompiles = context.journal_mut().precompile_addresses().is_empty();
35
36 if precompiles_changed || empty_warmed_precompiles {
37 context
40 .journal_mut()
41 .warm_precompiles(precompiles.warm_addresses().collect());
42 }
43
44 if spec.is_enabled_in(SpecId::SHANGHAI) {
47 let coinbase = context.block().beneficiary();
48 context.journal_mut().warm_coinbase_account(coinbase);
49 }
50
51 let (tx, journal) = context.tx_journal_mut();
53 if tx.tx_type() != TransactionType::Legacy {
55 if let Some(access_list) = tx.access_list() {
56 for item in access_list {
57 journal.warm_account_and_storage(
58 *item.address(),
59 item.storage_slots().map(|i| StorageKey::from_be_bytes(i.0)),
60 )?;
61 }
62 }
63 }
64
65 Ok(())
66}
67
68#[inline]
70pub fn validate_account_nonce_and_code(
71 caller_info: &mut AccountInfo,
72 tx_nonce: u64,
73 is_eip3607_disabled: bool,
74 is_nonce_check_disabled: bool,
75) -> Result<(), InvalidTransaction> {
76 if !is_eip3607_disabled {
80 let bytecode = match caller_info.code.as_ref() {
81 Some(code) => code,
82 None => &Bytecode::default(),
83 };
84 if !bytecode.is_empty() && !bytecode.is_eip7702() {
87 return Err(InvalidTransaction::RejectCallerWithCode);
88 }
89 }
90
91 if !is_nonce_check_disabled {
93 let tx = tx_nonce;
94 let state = caller_info.nonce;
95 match tx.cmp(&state) {
96 Ordering::Greater => {
97 return Err(InvalidTransaction::NonceTooHigh { tx, state });
98 }
99 Ordering::Less => {
100 return Err(InvalidTransaction::NonceTooLow { tx, state });
101 }
102 _ => {}
103 }
104 }
105 Ok(())
106}
107
108#[inline]
110pub fn validate_against_state_and_deduct_caller<
111 CTX: ContextTr,
112 ERROR: From<InvalidTransaction> + From<<CTX::Db as Database>::Error>,
113>(
114 context: &mut CTX,
115) -> Result<(), ERROR> {
116 let basefee = context.block().basefee() as u128;
117 let blob_price = context.block().blob_gasprice().unwrap_or_default();
118 let is_balance_check_disabled = context.cfg().is_balance_check_disabled();
119 let is_eip3607_disabled = context.cfg().is_eip3607_disabled();
120 let is_nonce_check_disabled = context.cfg().is_nonce_check_disabled();
121
122 let (tx, journal) = context.tx_journal_mut();
123
124 let caller_account = journal.load_account_code(tx.caller())?.data;
126
127 validate_account_nonce_and_code(
128 &mut caller_account.info,
129 tx.nonce(),
130 is_eip3607_disabled,
131 is_nonce_check_disabled,
132 )?;
133
134 if !is_balance_check_disabled {
135 tx.ensure_enough_balance(caller_account.info.balance)?;
136 }
137
138 let gas_balance_spending = tx
140 .gas_balance_spending(basefee, blob_price)
141 .expect("effective balance is always smaller than max balance so it can't overflow");
142
143 let mut new_balance = caller_account
144 .info
145 .balance
146 .saturating_sub(gas_balance_spending);
147
148 if is_balance_check_disabled {
149 new_balance = new_balance.max(tx.value());
151 }
152
153 let old_balance = caller_account.caller_initial_modification(new_balance, tx.kind().is_call());
154
155 journal.caller_accounting_journal_entry(tx.caller(), old_balance, tx.kind().is_call());
156 Ok(())
157}
158
159#[inline]
161pub fn apply_eip7702_auth_list<
162 CTX: ContextTr,
163 ERROR: From<InvalidTransaction> + From<<CTX::Db as Database>::Error>,
164>(
165 context: &mut CTX,
166) -> Result<u64, ERROR> {
167 let tx = context.tx();
168 if tx.tx_type() != TransactionType::Eip7702 {
170 return Ok(0);
171 }
172
173 let chain_id = context.cfg().chain_id();
174 let (tx, journal) = context.tx_journal_mut();
175
176 let mut refunded_accounts = 0;
177 for authorization in tx.authorization_list() {
178 let auth_chain_id = authorization.chain_id();
180 if !auth_chain_id.is_zero() && auth_chain_id != U256::from(chain_id) {
181 continue;
182 }
183
184 if authorization.nonce() == u64::MAX {
186 continue;
187 }
188
189 let Some(authority) = authorization.authority() else {
192 continue;
193 };
194
195 let mut authority_acc = journal.load_account_code(authority)?;
198
199 if let Some(bytecode) = &authority_acc.info.code {
201 if !bytecode.is_empty() && !bytecode.is_eip7702() {
203 continue;
204 }
205 }
206
207 if authorization.nonce() != authority_acc.info.nonce {
209 continue;
210 }
211
212 if !(authority_acc.is_empty() && authority_acc.is_loaded_as_not_existing_not_touched()) {
214 refunded_accounts += 1;
215 }
216
217 let address = authorization.address();
221 let (bytecode, hash) = if address.is_zero() {
222 (Bytecode::default(), KECCAK_EMPTY)
223 } else {
224 let bytecode = Bytecode::new_eip7702(address);
225 let hash = bytecode.hash_slow();
226 (bytecode, hash)
227 };
228 authority_acc.info.code_hash = hash;
229 authority_acc.info.code = Some(bytecode);
230
231 authority_acc.info.nonce = authority_acc.info.nonce.saturating_add(1);
233 authority_acc.mark_touch();
234 }
235
236 let refunded_gas =
237 refunded_accounts * (eip7702::PER_EMPTY_ACCOUNT_COST - eip7702::PER_AUTH_BASE_COST);
238
239 Ok(refunded_gas)
240}