revm_handler/
pre_execution.rsuse bytecode::Bytecode;
use context_interface::{
journaled_state::JournaledState,
result::InvalidTransaction,
transaction::{
eip7702::Authorization, AccessListTrait, Eip4844Tx, Eip7702Tx, Transaction, TransactionType,
},
Block, BlockGetter, Cfg, CfgGetter, JournalStateGetter, JournalStateGetterDBError,
TransactionGetter,
};
use handler_interface::PreExecutionHandler;
use primitives::{Address, BLOCKHASH_STORAGE_ADDRESS, U256};
use specification::{eip7702, hardfork::SpecId};
use std::{boxed::Box, vec::Vec};
#[derive(Default)]
pub struct EthPreExecution<CTX, ERROR> {
pub _phantom: core::marker::PhantomData<(CTX, ERROR)>,
}
impl<CTX, ERROR> EthPreExecution<CTX, ERROR> {
pub fn new() -> Self {
Self {
_phantom: core::marker::PhantomData,
}
}
pub fn new_boxed() -> Box<Self> {
Box::new(Self::new())
}
}
impl<CTX, ERROR> PreExecutionHandler for EthPreExecution<CTX, ERROR>
where
CTX: EthPreExecutionContext,
ERROR: EthPreExecutionError<CTX>,
{
type Context = CTX;
type Error = ERROR;
fn load_accounts(&self, context: &mut Self::Context) -> Result<(), Self::Error> {
let spec = context.cfg().spec().into();
context.journal().set_spec_id(spec);
if spec.is_enabled_in(SpecId::SHANGHAI) {
let coinbase = *context.block().beneficiary();
context.journal().warm_account(coinbase);
}
if spec.is_enabled_in(SpecId::PRAGUE) {
context.journal().warm_account(BLOCKHASH_STORAGE_ADDRESS);
}
if let Some(access_list) = context.tx().access_list().cloned() {
for access_list in access_list.iter() {
context.journal().warm_account_and_storage(
access_list.0,
access_list.1.map(|i| U256::from_be_bytes(i.0)),
)?;
}
};
Ok(())
}
fn apply_eip7702_auth_list(&self, context: &mut Self::Context) -> Result<u64, Self::Error> {
let spec = context.cfg().spec().into();
if spec.is_enabled_in(SpecId::PRAGUE) {
apply_eip7702_auth_list::<CTX, ERROR>(context)
} else {
Ok(0)
}
}
#[inline]
fn deduct_caller(&self, context: &mut Self::Context) -> Result<(), Self::Error> {
let basefee = *context.block().basefee();
let blob_price = U256::from(context.block().blob_gasprice().unwrap_or_default());
let effective_gas_price = context.tx().effective_gas_price(basefee);
let mut gas_cost = U256::from(context.tx().common_fields().gas_limit())
.saturating_mul(effective_gas_price);
if context.tx().tx_type().into() == TransactionType::Eip4844 {
let blob_gas = U256::from(context.tx().eip4844().total_blob_gas());
gas_cost = gas_cost.saturating_add(blob_price.saturating_mul(blob_gas));
}
let is_call = context.tx().kind().is_call();
let caller = context.tx().common_fields().caller();
let caller_account = context.journal().load_account(caller)?.data;
caller_account.info.balance = caller_account.info.balance.saturating_sub(gas_cost);
if is_call {
caller_account.info.nonce = caller_account.info.nonce.saturating_add(1);
}
caller_account.mark_touch();
Ok(())
}
}
#[inline]
pub fn apply_eip7702_auth_list<
CTX: TransactionGetter + JournalStateGetter + CfgGetter,
ERROR: From<InvalidTransaction> + From<JournalStateGetterDBError<CTX>>,
>(
context: &mut CTX,
) -> Result<u64, ERROR> {
let tx = context.tx();
if tx.tx_type().into() != TransactionType::Eip7702 {
return Ok(0);
}
struct Authorization {
authority: Option<Address>,
address: Address,
nonce: u64,
chain_id: u64,
}
let authorization_list = tx
.eip7702()
.authorization_list_iter()
.map(|a| Authorization {
authority: a.authority(),
address: a.address(),
nonce: a.nonce(),
chain_id: a.chain_id(),
})
.collect::<Vec<_>>();
let chain_id = context.cfg().chain_id();
let mut refunded_accounts = 0;
for authorization in authorization_list {
let Some(authority) = authorization.authority else {
continue;
};
if authorization.chain_id != 0 && authorization.chain_id != chain_id {
continue;
}
let mut authority_acc = context.journal().load_account_code(authority)?;
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.is_empty() {
refunded_accounts += 1;
}
let bytecode = Bytecode::new_eip7702(authorization.address);
authority_acc.info.code_hash = bytecode.hash_slow();
authority_acc.info.code = Some(bytecode);
authority_acc.info.nonce = authority_acc.info.nonce.saturating_add(1);
authority_acc.mark_touch();
}
let refunded_gas =
refunded_accounts * (eip7702::PER_EMPTY_ACCOUNT_COST - eip7702::PER_AUTH_BASE_COST);
Ok(refunded_gas)
}
pub trait EthPreExecutionContext:
TransactionGetter + BlockGetter + JournalStateGetter + CfgGetter
{
}
impl<CTX: TransactionGetter + BlockGetter + JournalStateGetter + CfgGetter> EthPreExecutionContext
for CTX
{
}
pub trait EthPreExecutionError<CTX: JournalStateGetter>:
From<InvalidTransaction> + From<JournalStateGetterDBError<CTX>>
{
}
impl<
CTX: JournalStateGetter,
T: From<InvalidTransaction> + From<JournalStateGetterDBError<CTX>>,
> EthPreExecutionError<CTX> for T
{
}