use crate::{
api::exec::OpContextTr,
constants::{BASE_FEE_RECIPIENT, L1_FEE_RECIPIENT, OPERATOR_FEE_RECIPIENT},
transaction::{deposit::DEPOSIT_TRANSACTION_TYPE, OpTransactionError, OpTxTr},
L1BlockInfo, OpHaltReason, OpSpecId,
};
use revm::{
context::{result::InvalidTransaction, LocalContextTr},
context_interface::{
result::{EVMError, ExecutionResult, FromStringError, ResultAndState},
Block, Cfg, ContextTr, JournalTr, Transaction,
},
handler::{
handler::EvmTrError, pre_execution::validate_account_nonce_and_code, EvmTr, Frame,
FrameResult, Handler, MainnetHandler,
},
inspector::{Inspector, InspectorEvmTr, InspectorFrame, InspectorHandler},
interpreter::{interpreter::EthInterpreter, FrameInput, Gas},
primitives::{hardfork::SpecId, HashMap, U256},
state::Account,
Database,
};
use std::boxed::Box;
pub struct OpHandler<EVM, ERROR, FRAME> {
pub mainnet: MainnetHandler<EVM, ERROR, FRAME>,
pub _phantom: core::marker::PhantomData<(EVM, ERROR, FRAME)>,
}
impl<EVM, ERROR, FRAME> OpHandler<EVM, ERROR, FRAME> {
pub fn new() -> Self {
Self {
mainnet: MainnetHandler::default(),
_phantom: core::marker::PhantomData,
}
}
}
impl<EVM, ERROR, FRAME> Default for OpHandler<EVM, ERROR, FRAME> {
fn default() -> Self {
Self::new()
}
}
pub trait IsTxError {
fn is_tx_error(&self) -> bool;
}
impl<DB, TX> IsTxError for EVMError<DB, TX> {
fn is_tx_error(&self) -> bool {
matches!(self, EVMError::Transaction(_))
}
}
impl<EVM, ERROR, FRAME> Handler for OpHandler<EVM, ERROR, FRAME>
where
EVM: EvmTr<Context: OpContextTr>,
ERROR: EvmTrError<EVM> + From<OpTransactionError> + FromStringError + IsTxError,
FRAME: Frame<Evm = EVM, Error = ERROR, FrameResult = FrameResult, FrameInit = FrameInput>,
{
type Evm = EVM;
type Error = ERROR;
type Frame = FRAME;
type HaltReason = OpHaltReason;
fn validate_env(&self, evm: &mut Self::Evm) -> Result<(), Self::Error> {
let ctx = evm.ctx();
let tx = ctx.tx();
let tx_type = tx.tx_type();
if tx_type == DEPOSIT_TRANSACTION_TYPE {
if tx.is_system_transaction()
&& evm.ctx().cfg().spec().is_enabled_in(OpSpecId::REGOLITH)
{
return Err(OpTransactionError::DepositSystemTxPostRegolith.into());
}
return Ok(());
}
self.mainnet.validate_env(evm)
}
fn validate_against_state_and_deduct_caller(
&self,
evm: &mut Self::Evm,
) -> Result<(), Self::Error> {
let ctx = evm.ctx();
let basefee = ctx.block().basefee() as u128;
let blob_price = ctx.block().blob_gasprice().unwrap_or_default();
let is_deposit = ctx.tx().tx_type() == DEPOSIT_TRANSACTION_TYPE;
let spec = ctx.cfg().spec();
let block_number = ctx.block().number();
let is_balance_check_disabled = ctx.cfg().is_balance_check_disabled();
let is_eip3607_disabled = ctx.cfg().is_eip3607_disabled();
let is_nonce_check_disabled = ctx.cfg().is_nonce_check_disabled();
let mint = ctx.tx().mint();
let mut additional_cost = U256::ZERO;
if !is_deposit {
if ctx.chain().l2_block != block_number {
*ctx.chain() = L1BlockInfo::try_fetch(ctx.db(), block_number, spec)?;
}
let enveloped_tx = ctx
.tx()
.enveloped_tx()
.expect("all not deposit tx have enveloped tx")
.clone();
additional_cost = ctx.chain().calculate_tx_l1_cost(&enveloped_tx, spec);
if spec.is_enabled_in(OpSpecId::ISTHMUS) {
let gas_limit = U256::from(ctx.tx().gas_limit());
let operator_fee_charge = ctx.chain().operator_fee_charge(&enveloped_tx, gas_limit);
additional_cost = additional_cost.saturating_add(operator_fee_charge);
}
}
let (tx, journal) = ctx.tx_journal();
let caller_account = journal.load_account_code(tx.caller())?.data;
if is_deposit {
if let Some(mint) = mint {
caller_account.info.balance =
caller_account.info.balance.saturating_add(U256::from(mint));
}
if tx.kind().is_call() {
caller_account.info.nonce = caller_account.info.nonce.saturating_add(1);
}
} else {
validate_account_nonce_and_code(
&mut caller_account.info,
tx.nonce(),
tx.kind().is_call(),
is_eip3607_disabled,
is_nonce_check_disabled,
)?;
}
let max_balance_spending = tx.max_balance_spending()?.saturating_add(additional_cost);
if is_balance_check_disabled {
caller_account.info.balance = caller_account.info.balance.max(tx.value());
} else if !is_deposit && max_balance_spending > caller_account.info.balance {
return Err(InvalidTransaction::LackOfFundForMaxFee {
fee: Box::new(max_balance_spending),
balance: Box::new(caller_account.info.balance),
}
.into());
} else {
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 op_gas_balance_spending = gas_balance_spending.saturating_add(additional_cost);
caller_account.info.balance = caller_account
.info
.balance
.saturating_sub(op_gas_balance_spending);
}
caller_account.mark_touch();
Ok(())
}
fn last_frame_result(
&mut self,
evm: &mut Self::Evm,
frame_result: &mut <Self::Frame as Frame>::FrameResult,
) -> Result<(), Self::Error> {
let ctx = evm.ctx();
let tx = ctx.tx();
let is_deposit = tx.tx_type() == DEPOSIT_TRANSACTION_TYPE;
let tx_gas_limit = tx.gas_limit();
let is_regolith = ctx.cfg().spec().is_enabled_in(OpSpecId::REGOLITH);
let instruction_result = frame_result.interpreter_result().result;
let gas = frame_result.gas_mut();
let remaining = gas.remaining();
let refunded = gas.refunded();
*gas = Gas::new_spent(tx_gas_limit);
if instruction_result.is_ok() {
if !is_deposit || is_regolith {
gas.erase_cost(remaining);
gas.record_refund(refunded);
} else if is_deposit {
let tx = ctx.tx();
if tx.is_system_transaction() {
gas.erase_cost(tx_gas_limit);
}
}
} else if instruction_result.is_revert() {
if !is_deposit || is_regolith {
gas.erase_cost(remaining);
}
}
Ok(())
}
fn reimburse_caller(
&self,
evm: &mut Self::Evm,
exec_result: &mut <Self::Frame as Frame>::FrameResult,
) -> Result<(), Self::Error> {
self.mainnet.reimburse_caller(evm, exec_result)?;
let context = evm.ctx();
if context.tx().tx_type() != DEPOSIT_TRANSACTION_TYPE {
let caller = context.tx().caller();
let spec = context.cfg().spec();
let operator_fee_refund = context.chain().operator_fee_refund(exec_result.gas(), spec);
let caller_account = context.journal().load_account(caller)?;
caller_account.data.info.balance = caller_account
.data
.info
.balance
.saturating_add(operator_fee_refund);
}
Ok(())
}
fn refund(
&self,
evm: &mut Self::Evm,
exec_result: &mut <Self::Frame as Frame>::FrameResult,
eip7702_refund: i64,
) {
exec_result.gas_mut().record_refund(eip7702_refund);
let is_deposit = evm.ctx().tx().tx_type() == DEPOSIT_TRANSACTION_TYPE;
let is_regolith = evm.ctx().cfg().spec().is_enabled_in(OpSpecId::REGOLITH);
let is_gas_refund_disabled = is_deposit && !is_regolith;
if !is_gas_refund_disabled {
exec_result.gas_mut().set_final_refund(
evm.ctx()
.cfg()
.spec()
.into_eth_spec()
.is_enabled_in(SpecId::LONDON),
);
}
}
fn reward_beneficiary(
&self,
evm: &mut Self::Evm,
exec_result: &mut <Self::Frame as Frame>::FrameResult,
) -> Result<(), Self::Error> {
let is_deposit = evm.ctx().tx().tx_type() == DEPOSIT_TRANSACTION_TYPE;
if !is_deposit {
self.mainnet.reward_beneficiary(evm, exec_result)?;
let basefee = evm.ctx().block().basefee() as u128;
let ctx = evm.ctx();
let enveloped = ctx.tx().enveloped_tx().cloned();
let spec = ctx.cfg().spec();
let l1_block_info = ctx.chain();
let Some(enveloped_tx) = &enveloped else {
return Err(ERROR::from_string(
"[OPTIMISM] Failed to load enveloped transaction.".into(),
));
};
let l1_cost = l1_block_info.calculate_tx_l1_cost(enveloped_tx, spec);
let mut operator_fee_cost = U256::ZERO;
if spec.is_enabled_in(OpSpecId::ISTHMUS) {
operator_fee_cost = l1_block_info.operator_fee_charge(
enveloped_tx,
U256::from(exec_result.gas().spent() - exec_result.gas().refunded() as u64),
);
}
let mut l1_fee_vault_account = ctx.journal().load_account(L1_FEE_RECIPIENT)?;
l1_fee_vault_account.mark_touch();
l1_fee_vault_account.info.balance += l1_cost;
let mut base_fee_vault_account =
evm.ctx().journal().load_account(BASE_FEE_RECIPIENT)?;
base_fee_vault_account.mark_touch();
base_fee_vault_account.info.balance += U256::from(basefee.saturating_mul(
(exec_result.gas().spent() - exec_result.gas().refunded() as u64) as u128,
));
let mut operator_fee_vault_account =
evm.ctx().journal().load_account(OPERATOR_FEE_RECIPIENT)?;
operator_fee_vault_account.mark_touch();
operator_fee_vault_account.data.info.balance += operator_fee_cost;
}
Ok(())
}
fn output(
&self,
evm: &mut Self::Evm,
result: <Self::Frame as Frame>::FrameResult,
) -> Result<ResultAndState<Self::HaltReason>, Self::Error> {
let result = self.mainnet.output(evm, result)?;
let result = result.map_haltreason(OpHaltReason::Base);
if result.result.is_halt() {
let is_deposit = evm.ctx().tx().tx_type() == DEPOSIT_TRANSACTION_TYPE;
if is_deposit && evm.ctx().cfg().spec().is_enabled_in(OpSpecId::REGOLITH) {
return Err(ERROR::from(OpTransactionError::HaltedDepositPostRegolith));
}
}
evm.ctx().chain().clear_tx_l1_cost();
Ok(result)
}
fn catch_error(
&self,
evm: &mut Self::Evm,
error: Self::Error,
) -> Result<ResultAndState<Self::HaltReason>, Self::Error> {
let is_deposit = evm.ctx().tx().tx_type() == DEPOSIT_TRANSACTION_TYPE;
let output = if error.is_tx_error() && is_deposit {
let ctx = evm.ctx();
let spec = ctx.cfg().spec();
let tx = ctx.tx();
let caller = tx.caller();
let mint = tx.mint();
let is_system_tx = tx.is_system_transaction();
let gas_limit = tx.gas_limit();
let account = {
let mut acc = Account::from(
evm.ctx()
.db()
.basic(caller)
.unwrap_or_default()
.unwrap_or_default(),
);
acc.info.nonce = acc.info.nonce.saturating_add(1);
acc.info.balance = acc
.info
.balance
.saturating_add(U256::from(mint.unwrap_or_default()));
acc.mark_touch();
acc
};
let state = HashMap::from_iter([(caller, account)]);
let gas_used = if spec.is_enabled_in(OpSpecId::REGOLITH) || !is_system_tx {
gas_limit
} else {
0
};
Ok(ResultAndState {
result: ExecutionResult::Halt {
reason: OpHaltReason::FailedDeposit,
gas_used,
},
state,
})
} else {
Err(error)
};
evm.ctx().chain().clear_tx_l1_cost();
evm.ctx().journal().clear();
evm.ctx().local().clear();
output
}
}
impl<EVM, ERROR, FRAME> InspectorHandler for OpHandler<EVM, ERROR, FRAME>
where
EVM: InspectorEvmTr<
Context: OpContextTr,
Inspector: Inspector<<<Self as Handler>::Evm as EvmTr>::Context, EthInterpreter>,
>,
ERROR: EvmTrError<EVM> + From<OpTransactionError> + FromStringError + IsTxError,
FRAME: InspectorFrame<
Evm = EVM,
Error = ERROR,
FrameResult = FrameResult,
FrameInit = FrameInput,
IT = EthInterpreter,
>,
{
type IT = EthInterpreter;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
api::default_ctx::OpContext,
constants::{
BASE_FEE_SCALAR_OFFSET, ECOTONE_L1_BLOB_BASE_FEE_SLOT, ECOTONE_L1_FEE_SCALARS_SLOT,
L1_BASE_FEE_SLOT, L1_BLOCK_CONTRACT, OPERATOR_FEE_SCALARS_SLOT,
},
DefaultOp, OpBuilder,
};
use alloy_primitives::uint;
use revm::{
context::{BlockEnv, Context, TransactionType},
context_interface::result::InvalidTransaction,
database::InMemoryDB,
database_interface::EmptyDB,
handler::EthFrame,
interpreter::{CallOutcome, InstructionResult, InterpreterResult},
primitives::{bytes, Address, Bytes, B256},
state::AccountInfo,
};
use rstest::rstest;
use std::boxed::Box;
fn call_last_frame_return(
ctx: OpContext<EmptyDB>,
instruction_result: InstructionResult,
gas: Gas,
) -> Gas {
let mut evm = ctx.build_op();
let mut exec_result = FrameResult::Call(CallOutcome::new(
InterpreterResult {
result: instruction_result,
output: Bytes::new(),
gas,
},
0..0,
));
let mut handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
handler
.last_frame_result(&mut evm, &mut exec_result)
.unwrap();
handler.refund(&mut evm, &mut exec_result, 0);
*exec_result.gas()
}
#[test]
fn test_revert_gas() {
let ctx = Context::op()
.modify_tx_chained(|tx| {
tx.base.gas_limit = 100;
tx.enveloped_tx = None;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::BEDROCK);
let gas = call_last_frame_return(ctx, InstructionResult::Revert, Gas::new(90));
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spent(), 10);
assert_eq!(gas.refunded(), 0);
}
#[test]
fn test_consume_gas() {
let ctx = Context::op()
.modify_tx_chained(|tx| {
tx.base.gas_limit = 100;
tx.deposit.source_hash = B256::ZERO;
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);
let gas = call_last_frame_return(ctx, InstructionResult::Stop, Gas::new(90));
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spent(), 10);
assert_eq!(gas.refunded(), 0);
}
#[test]
fn test_consume_gas_with_refund() {
let ctx = Context::op()
.modify_tx_chained(|tx| {
tx.base.gas_limit = 100;
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.deposit.source_hash = B256::ZERO;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);
let mut ret_gas = Gas::new(90);
ret_gas.record_refund(20);
let gas = call_last_frame_return(ctx.clone(), InstructionResult::Stop, ret_gas);
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spent(), 10);
assert_eq!(gas.refunded(), 2);
let gas = call_last_frame_return(ctx, InstructionResult::Revert, ret_gas);
assert_eq!(gas.remaining(), 90);
assert_eq!(gas.spent(), 10);
assert_eq!(gas.refunded(), 0);
}
#[test]
fn test_consume_gas_deposit_tx() {
let ctx = Context::op()
.modify_tx_chained(|tx| {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.base.gas_limit = 100;
tx.deposit.source_hash = B256::ZERO;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::BEDROCK);
let gas = call_last_frame_return(ctx, InstructionResult::Stop, Gas::new(90));
assert_eq!(gas.remaining(), 0);
assert_eq!(gas.spent(), 100);
assert_eq!(gas.refunded(), 0);
}
#[test]
fn test_consume_gas_sys_deposit_tx() {
let ctx = Context::op()
.modify_tx_chained(|tx| {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.base.gas_limit = 100;
tx.deposit.source_hash = B256::ZERO;
tx.deposit.is_system_transaction = true;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::BEDROCK);
let gas = call_last_frame_return(ctx, InstructionResult::Stop, Gas::new(90));
assert_eq!(gas.remaining(), 100);
assert_eq!(gas.spent(), 0);
assert_eq!(gas.refunded(), 0);
}
#[test]
fn test_commit_mint_value() {
let caller = Address::ZERO;
let mut db = InMemoryDB::default();
db.insert_account_info(
caller,
AccountInfo {
balance: U256::from(1000),
..Default::default()
},
);
let mut ctx = Context::op()
.with_db(db)
.with_chain(L1BlockInfo {
l1_base_fee: U256::from(1_000),
l1_fee_overhead: Some(U256::from(1_000)),
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);
ctx.modify_tx(|tx| {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.deposit.source_hash = B256::ZERO;
tx.deposit.mint = Some(10);
});
let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
handler
.validate_against_state_and_deduct_caller(&mut evm)
.unwrap();
let account = evm.ctx().journal().load_account(caller).unwrap();
assert_eq!(account.info.balance, U256::from(1010));
}
#[test]
fn test_remove_l1_cost_non_deposit() {
let caller = Address::ZERO;
let mut db = InMemoryDB::default();
db.insert_account_info(
caller,
AccountInfo {
balance: U256::from(1000),
..Default::default()
},
);
let ctx = Context::op()
.with_db(db)
.with_chain(L1BlockInfo {
l1_base_fee: U256::from(1_000),
l1_fee_overhead: Some(U256::from(1_000)),
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH)
.modify_tx_chained(|tx| {
tx.base.gas_limit = 100;
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.deposit.mint = Some(10);
tx.enveloped_tx = Some(bytes!("FACADE"));
tx.deposit.source_hash = B256::ZERO;
});
let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
handler
.validate_against_state_and_deduct_caller(&mut evm)
.unwrap();
let account = evm.ctx().journal().load_account(caller).unwrap();
assert_eq!(account.info.balance, U256::from(1010));
}
#[test]
fn test_reload_l1_block_info_isthmus() {
const BLOCK_NUM: u64 = 100;
const L1_BASE_FEE: U256 = uint!(1_U256);
const L1_BLOB_BASE_FEE: U256 = uint!(2_U256);
const L1_BASE_FEE_SCALAR: u64 = 3;
const L1_BLOB_BASE_FEE_SCALAR: u64 = 4;
const L1_FEE_SCALARS: U256 = U256::from_limbs([
0,
(L1_BASE_FEE_SCALAR << (64 - BASE_FEE_SCALAR_OFFSET * 2)) | L1_BLOB_BASE_FEE_SCALAR,
0,
0,
]);
const OPERATOR_FEE_SCALAR: u64 = 5;
const OPERATOR_FEE_CONST: u64 = 6;
const OPERATOR_FEE: U256 =
U256::from_limbs([OPERATOR_FEE_CONST, OPERATOR_FEE_SCALAR, 0, 0]);
let mut db = InMemoryDB::default();
let l1_block_contract = db.load_account(L1_BLOCK_CONTRACT).unwrap();
l1_block_contract
.storage
.insert(L1_BASE_FEE_SLOT, L1_BASE_FEE);
l1_block_contract
.storage
.insert(ECOTONE_L1_BLOB_BASE_FEE_SLOT, L1_BLOB_BASE_FEE);
l1_block_contract
.storage
.insert(ECOTONE_L1_FEE_SCALARS_SLOT, L1_FEE_SCALARS);
l1_block_contract
.storage
.insert(OPERATOR_FEE_SCALARS_SLOT, OPERATOR_FEE);
db.insert_account_info(
Address::ZERO,
AccountInfo {
balance: U256::from(1000),
..Default::default()
},
);
let ctx = Context::op()
.with_db(db)
.with_chain(L1BlockInfo {
l2_block: BLOCK_NUM + 1, ..Default::default()
})
.with_block(BlockEnv {
number: BLOCK_NUM,
..Default::default()
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS);
let mut evm = ctx.build_op();
assert_ne!(evm.ctx().chain().l2_block, BLOCK_NUM);
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
handler
.validate_against_state_and_deduct_caller(&mut evm)
.unwrap();
assert_eq!(
*evm.ctx().chain(),
L1BlockInfo {
l2_block: BLOCK_NUM,
l1_base_fee: L1_BASE_FEE,
l1_base_fee_scalar: U256::from(L1_BASE_FEE_SCALAR),
l1_blob_base_fee: Some(L1_BLOB_BASE_FEE),
l1_blob_base_fee_scalar: Some(U256::from(L1_BLOB_BASE_FEE_SCALAR)),
empty_ecotone_scalars: false,
l1_fee_overhead: None,
operator_fee_scalar: Some(U256::from(OPERATOR_FEE_SCALAR)),
operator_fee_constant: Some(U256::from(OPERATOR_FEE_CONST)),
tx_l1_cost: Some(U256::ZERO),
}
);
}
#[test]
fn test_remove_l1_cost() {
let caller = Address::ZERO;
let mut db = InMemoryDB::default();
db.insert_account_info(
caller,
AccountInfo {
balance: U256::from(1049),
..Default::default()
},
);
let ctx = Context::op()
.with_db(db)
.with_chain(L1BlockInfo {
l1_base_fee: U256::from(1_000),
l1_fee_overhead: Some(U256::from(1_000)),
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH)
.modify_tx_chained(|tx| {
tx.base.gas_limit = 100;
tx.deposit.source_hash = B256::ZERO;
tx.enveloped_tx = Some(bytes!("FACADE"));
});
let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
handler
.validate_against_state_and_deduct_caller(&mut evm)
.unwrap();
let account = evm.ctx().journal().load_account(caller).unwrap();
assert_eq!(account.info.balance, U256::from(1));
}
#[test]
fn test_remove_operator_cost() {
let caller = Address::ZERO;
let mut db = InMemoryDB::default();
db.insert_account_info(
caller,
AccountInfo {
balance: U256::from(151),
..Default::default()
},
);
let ctx = Context::op()
.with_db(db)
.with_chain(L1BlockInfo {
operator_fee_scalar: Some(U256::from(10_000_000)),
operator_fee_constant: Some(U256::from(50)),
..Default::default()
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS)
.modify_tx_chained(|tx| {
tx.base.gas_limit = 10;
tx.enveloped_tx = Some(bytes!("FACADE"));
});
let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
handler
.validate_against_state_and_deduct_caller(&mut evm)
.unwrap();
let account = evm.ctx().journal().load_account(caller).unwrap();
assert_eq!(account.info.balance, U256::from(1));
}
#[test]
fn test_remove_l1_cost_lack_of_funds() {
let caller = Address::ZERO;
let mut db = InMemoryDB::default();
db.insert_account_info(
caller,
AccountInfo {
balance: U256::from(48),
..Default::default()
},
);
let ctx = Context::op()
.with_db(db)
.with_chain(L1BlockInfo {
l1_base_fee: U256::from(1_000),
l1_fee_overhead: Some(U256::from(1_000)),
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH)
.modify_tx_chained(|tx| {
tx.enveloped_tx = Some(bytes!("FACADE"));
});
let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
assert_eq!(
handler.validate_against_state_and_deduct_caller(&mut evm),
Err(EVMError::Transaction(
InvalidTransaction::LackOfFundForMaxFee {
fee: Box::new(U256::from(1048)),
balance: Box::new(U256::from(48)),
}
.into(),
))
);
}
#[test]
fn test_validate_sys_tx() {
let ctx = Context::op()
.modify_tx_chained(|tx| {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.deposit.is_system_transaction = true;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);
let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
assert_eq!(
handler.validate_env(&mut evm),
Err(EVMError::Transaction(
OpTransactionError::DepositSystemTxPostRegolith
))
);
evm.ctx().modify_cfg(|cfg| cfg.spec = OpSpecId::BEDROCK);
assert!(handler.validate_env(&mut evm).is_ok());
}
#[test]
fn test_validate_deposit_tx() {
let ctx = Context::op()
.modify_tx_chained(|tx| {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.deposit.source_hash = B256::ZERO;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);
let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
assert!(handler.validate_env(&mut evm).is_ok());
}
#[test]
fn test_validate_tx_against_state_deposit_tx() {
let ctx = Context::op()
.modify_tx_chained(|tx| {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.deposit.source_hash = B256::ZERO;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);
let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
assert!(handler.validate_env(&mut evm).is_ok());
}
#[test]
fn test_halted_deposit_tx_post_regolith() {
let ctx = Context::op()
.modify_tx_chained(|tx| {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);
let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
assert_eq!(
handler.output(
&mut evm,
FrameResult::Call(CallOutcome {
result: InterpreterResult {
result: InstructionResult::OutOfGas,
output: Default::default(),
gas: Default::default(),
},
memory_offset: Default::default(),
})
),
Err(EVMError::Transaction(
OpTransactionError::HaltedDepositPostRegolith
))
)
}
#[rstest]
#[case::deposit(true)]
#[case::dyn_fee(false)]
fn test_operator_fee_refund(#[case] is_deposit: bool) {
const SENDER: Address = Address::ZERO;
const GAS_PRICE: u128 = 0xFF;
const OP_FEE_MOCK_PARAM: u128 = 0xFFFF;
let ctx = Context::op()
.modify_tx_chained(|tx| {
tx.base.tx_type = if is_deposit {
DEPOSIT_TRANSACTION_TYPE
} else {
TransactionType::Eip1559 as u8
};
tx.base.gas_price = GAS_PRICE;
tx.base.gas_priority_fee = None;
tx.base.caller = SENDER;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS);
let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
evm.ctx().chain.operator_fee_scalar = Some(U256::from(OP_FEE_MOCK_PARAM));
evm.ctx().chain.operator_fee_constant = Some(U256::from(OP_FEE_MOCK_PARAM));
let mut gas = Gas::new(100);
gas.set_spent(10);
let mut exec_result = FrameResult::Call(CallOutcome::new(
InterpreterResult {
result: InstructionResult::Return,
output: Default::default(),
gas,
},
0..0,
));
handler
.reimburse_caller(&mut evm, &mut exec_result)
.unwrap();
let mut expected_refund =
U256::from(GAS_PRICE * (gas.remaining() + gas.refunded() as u64) as u128);
let op_fee_refund = evm
.ctx()
.chain()
.operator_fee_refund(&gas, OpSpecId::ISTHMUS);
assert!(op_fee_refund > U256::ZERO);
if !is_deposit {
expected_refund += op_fee_refund;
}
let account = evm.ctx().journal().load_account(SENDER).unwrap();
assert_eq!(account.info.balance, expected_refund);
}
}