use crate::{EvmTr, PrecompileProvider};
use bytecode::Bytecode;
use context_interface::{
journaled_state::{account::JournaledAccountTr, JournalTr},
result::InvalidTransaction,
transaction::{AccessListItemTr, AuthorizationTr, Transaction, TransactionType},
Block, Cfg, ContextTr, Database,
};
use core::cmp::Ordering;
use interpreter::InitialAndFloorGas;
use primitives::{hardfork::SpecId, AddressMap, HashSet, StorageKey, U256};
use state::AccountInfo;
pub fn load_accounts<
EVM: EvmTr<Precompiles: PrecompileProvider<EVM::Context>>,
ERROR: From<<<EVM::Context as ContextTr>::Db as Database>::Error>,
>(
evm: &mut EVM,
) -> Result<(), ERROR> {
let (context, precompiles) = evm.ctx_precompiles();
let gen_spec = context.cfg().spec();
let spec = gen_spec.clone().into();
context.journal_mut().set_spec_id(spec);
let precompiles_changed = precompiles.set_spec(gen_spec);
let empty_warmed_precompiles = context.journal_mut().precompile_addresses().is_empty();
if precompiles_changed || empty_warmed_precompiles {
context
.journal_mut()
.warm_precompiles(precompiles.warm_addresses().collect());
}
if spec.is_enabled_in(SpecId::SHANGHAI) {
let coinbase = context.block().beneficiary();
context.journal_mut().warm_coinbase_account(coinbase);
}
let (tx, journal) = context.tx_journal_mut();
if tx.tx_type() != TransactionType::Legacy {
if let Some(access_list) = tx.access_list() {
let mut map: AddressMap<HashSet<StorageKey>> = AddressMap::default();
for item in access_list {
map.entry(*item.address())
.or_default()
.extend(item.storage_slots().map(|key| U256::from_be_bytes(key.0)));
}
journal.warm_access_list(map);
}
}
Ok(())
}
#[inline]
pub fn validate_account_nonce_and_code_with_components(
caller_info: &AccountInfo,
tx: impl Transaction,
cfg: impl Cfg,
) -> Result<(), InvalidTransaction> {
validate_account_nonce_and_code(
caller_info,
tx.nonce(),
cfg.is_eip3607_disabled(),
cfg.is_nonce_check_disabled(),
)
}
#[inline]
pub fn validate_account_nonce_and_code(
caller_info: &AccountInfo,
tx_nonce: u64,
is_eip3607_disabled: bool,
is_nonce_check_disabled: bool,
) -> Result<(), InvalidTransaction> {
if !is_eip3607_disabled {
let bytecode = match caller_info.code.as_ref() {
Some(code) => code,
None => &Bytecode::default(),
};
if !bytecode.is_empty() && !bytecode.is_eip7702() {
return Err(InvalidTransaction::RejectCallerWithCode);
}
}
if !is_nonce_check_disabled {
let tx = tx_nonce;
let state = caller_info.nonce;
match tx.cmp(&state) {
Ordering::Greater => {
return Err(InvalidTransaction::NonceTooHigh { tx, state });
}
Ordering::Less => {
return Err(InvalidTransaction::NonceTooLow { tx, state });
}
_ => {}
}
}
Ok(())
}
#[inline]
pub fn calculate_caller_fee(
balance: U256,
tx: impl Transaction,
block: impl Block,
cfg: impl Cfg,
) -> Result<U256, InvalidTransaction> {
if cfg.is_fee_charge_disabled() {
return Ok(balance);
}
let basefee = block.basefee() as u128;
let blob_price = block.blob_gasprice().unwrap_or_default();
let is_balance_check_disabled = cfg.is_balance_check_disabled();
if !is_balance_check_disabled {
tx.ensure_enough_balance(balance)?;
}
let effective_balance_spending = tx
.effective_balance_spending(basefee, blob_price)
.expect("effective balance is always smaller than max balance so it can't overflow");
let gas_balance_spending = effective_balance_spending - tx.value();
let mut new_balance = balance.saturating_sub(gas_balance_spending);
if is_balance_check_disabled {
new_balance = new_balance.max(tx.value());
}
Ok(new_balance)
}
#[inline]
pub fn validate_against_state_and_deduct_caller<
CTX: ContextTr,
ERROR: From<InvalidTransaction> + From<<CTX::Db as Database>::Error>,
>(
context: &mut CTX,
) -> Result<(), ERROR> {
let (block, tx, cfg, journal, _, _) = context.all_mut();
let mut caller = journal.load_account_with_code_mut(tx.caller())?.data;
validate_account_nonce_and_code_with_components(&caller.account().info, tx, cfg)?;
let new_balance = calculate_caller_fee(*caller.balance(), tx, block, cfg)?;
caller.set_balance(new_balance);
if tx.kind().is_call() {
caller.bump_nonce();
}
Ok(())
}
#[inline]
pub fn apply_eip7702_auth_list<
CTX: ContextTr,
ERROR: From<InvalidTransaction> + From<<CTX::Db as Database>::Error>,
>(
context: &mut CTX,
init_and_floor_gas: &mut InitialAndFloorGas,
) -> Result<u64, ERROR> {
let chain_id = context.cfg().chain_id();
let refund_per_auth = context.cfg().gas_params().tx_eip7702_auth_refund();
let (tx, journal) = context.tx_journal_mut();
if tx.tx_type() != TransactionType::Eip7702 {
return Ok(0);
}
let eip7702_refund =
apply_auth_list::<_, ERROR>(chain_id, refund_per_auth, tx.authorization_list(), journal)?;
let (eip7702_state_refund, eip7702_regular_refund_raw) = context
.cfg()
.gas_params()
.split_eip7702_refund(eip7702_refund);
if eip7702_state_refund > 0 {
init_and_floor_gas.eip7702_reservoir_refund = eip7702_state_refund;
}
Ok(eip7702_regular_refund_raw)
}
#[inline]
pub fn apply_auth_list<
JOURNAL: JournalTr,
ERROR: From<InvalidTransaction> + From<<JOURNAL::Database as Database>::Error>,
>(
chain_id: u64,
refund_per_auth: u64,
auth_list: impl Iterator<Item = impl AuthorizationTr>,
journal: &mut JOURNAL,
) -> Result<u64, ERROR> {
let mut refunded_accounts = 0;
for authorization in auth_list {
let auth_chain_id = authorization.chain_id();
if !auth_chain_id.is_zero() && auth_chain_id != U256::from(chain_id) {
continue;
}
if authorization.nonce() == u64::MAX {
continue;
}
let Some(authority) = authorization.authority() else {
continue;
};
let mut authority_acc = journal.load_account_with_code_mut(authority)?;
let authority_acc_info = &authority_acc.account().info;
if let Some(bytecode) = &authority_acc_info.code {
if !bytecode.is_empty() && !bytecode.is_eip7702() {
continue;
}
}
if authorization.nonce() != authority_acc_info.nonce {
continue;
}
if !(authority_acc_info.is_empty()
&& authority_acc
.account()
.is_loaded_as_not_existing_not_touched())
{
refunded_accounts += 1;
}
authority_acc.delegate(authorization.address());
}
let refunded_gas = refunded_accounts * refund_per_auth;
Ok(refunded_gas)
}