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;
19use std::boxed::Box;
20
21pub 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_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 let address = item.address();
58 let mut storage = item.storage_slots().peekable();
59 if storage.peek().is_none() {
60 journal.warm_account(*address);
61 } else {
62 journal.warm_account_and_storage(
63 *address,
64 storage.map(|i| StorageKey::from_be_bytes(i.0)),
65 )?;
66 }
67 }
68 }
69 }
70
71 Ok(())
72}
73
74#[inline]
75pub fn validate_account_nonce_and_code(
76 caller_info: &mut AccountInfo,
77 tx_nonce: u64,
78 is_eip3607_disabled: bool,
79 is_nonce_check_disabled: bool,
80) -> Result<(), InvalidTransaction> {
81 if !is_eip3607_disabled {
85 let bytecode = match caller_info.code.as_ref() {
86 Some(code) => code,
87 None => &Bytecode::default(),
88 };
89 if !bytecode.is_empty() && !bytecode.is_eip7702() {
92 return Err(InvalidTransaction::RejectCallerWithCode);
93 }
94 }
95
96 if !is_nonce_check_disabled {
98 let tx = tx_nonce;
99 let state = caller_info.nonce;
100 match tx.cmp(&state) {
101 Ordering::Greater => {
102 return Err(InvalidTransaction::NonceTooHigh { tx, state });
103 }
104 Ordering::Less => {
105 return Err(InvalidTransaction::NonceTooLow { tx, state });
106 }
107 _ => {}
108 }
109 }
110 Ok(())
111}
112
113#[inline]
114pub fn validate_against_state_and_deduct_caller<
115 CTX: ContextTr,
116 ERROR: From<InvalidTransaction> + From<<CTX::Db as Database>::Error>,
117>(
118 context: &mut CTX,
119) -> Result<(), ERROR> {
120 let basefee = context.block().basefee() as u128;
121 let blob_price = context.block().blob_gasprice().unwrap_or_default();
122 let is_balance_check_disabled = context.cfg().is_balance_check_disabled();
123 let is_eip3607_disabled = context.cfg().is_eip3607_disabled();
124 let is_nonce_check_disabled = context.cfg().is_nonce_check_disabled();
125
126 let (tx, journal) = context.tx_journal_mut();
127
128 let caller_account = journal.load_account_code(tx.caller())?.data;
130
131 validate_account_nonce_and_code(
132 &mut caller_account.info,
133 tx.nonce(),
134 is_eip3607_disabled,
135 is_nonce_check_disabled,
136 )?;
137
138 if tx.kind().is_call() {
140 caller_account.info.nonce = caller_account.info.nonce.saturating_add(1);
142 }
143
144 let max_balance_spending = tx.max_balance_spending()?;
145
146 let mut new_balance = caller_account.info.balance;
147
148 if is_balance_check_disabled {
151 new_balance = caller_account.info.balance.max(tx.value());
153 } else if max_balance_spending > caller_account.info.balance {
154 return Err(InvalidTransaction::LackOfFundForMaxFee {
155 fee: Box::new(max_balance_spending),
156 balance: Box::new(caller_account.info.balance),
157 }
158 .into());
159 } else {
160 let effective_balance_spending = tx
161 .effective_balance_spending(basefee, blob_price)
162 .expect("effective balance is always smaller than max balance so it can't overflow");
163
164 let gas_balance_spending = effective_balance_spending - tx.value();
166
167 new_balance = new_balance.saturating_sub(gas_balance_spending);
168 }
169
170 let old_balance = caller_account.info.balance;
171 caller_account.mark_touch();
173 caller_account.info.balance = new_balance;
174
175 journal.caller_accounting_journal_entry(tx.caller(), old_balance, tx.kind().is_call());
176 Ok(())
177}
178
179#[inline]
181pub fn apply_eip7702_auth_list<
182 CTX: ContextTr,
183 ERROR: From<InvalidTransaction> + From<<CTX::Db as Database>::Error>,
184>(
185 context: &mut CTX,
186) -> Result<u64, ERROR> {
187 let tx = context.tx();
188 if tx.tx_type() != TransactionType::Eip7702 {
190 return Ok(0);
191 }
192
193 let chain_id = context.cfg().chain_id();
194 let (tx, journal) = context.tx_journal_mut();
195
196 let mut refunded_accounts = 0;
197 for authorization in tx.authorization_list() {
198 let auth_chain_id = authorization.chain_id();
200 if !auth_chain_id.is_zero() && auth_chain_id != U256::from(chain_id) {
201 continue;
202 }
203
204 if authorization.nonce() == u64::MAX {
206 continue;
207 }
208
209 let Some(authority) = authorization.authority() else {
212 continue;
213 };
214
215 let mut authority_acc = journal.load_account_code(authority)?;
218
219 if let Some(bytecode) = &authority_acc.info.code {
221 if !bytecode.is_empty() && !bytecode.is_eip7702() {
223 continue;
224 }
225 }
226
227 if authorization.nonce() != authority_acc.info.nonce {
229 continue;
230 }
231
232 if !(authority_acc.is_empty() && authority_acc.is_loaded_as_not_existing_not_touched()) {
234 refunded_accounts += 1;
235 }
236
237 let address = authorization.address();
241 let (bytecode, hash) = if address.is_zero() {
242 (Bytecode::default(), KECCAK_EMPTY)
243 } else {
244 let bytecode = Bytecode::new_eip7702(address);
245 let hash = bytecode.hash_slow();
246 (bytecode, hash)
247 };
248 authority_acc.info.code_hash = hash;
249 authority_acc.info.code = Some(bytecode);
250
251 authority_acc.info.nonce = authority_acc.info.nonce.saturating_add(1);
253 authority_acc.mark_touch();
254 }
255
256 let refunded_gas =
257 refunded_accounts * (eip7702::PER_EMPTY_ACCOUNT_COST - eip7702::PER_AUTH_BASE_COST);
258
259 Ok(refunded_gas)
260}