Skip to content

Commit

Permalink
fix(op): add operator fee scalar scaling factor on refund (#2050)
Browse files Browse the repository at this point in the history
  • Loading branch information
leruaa authored Feb 6, 2025
1 parent 20df4d7 commit 2c9290a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
5 changes: 3 additions & 2 deletions crates/revm/src/optimism/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub fn validate_tx_against_state<SPEC: Spec, EXT, DB: Database>(
.l1_block_info
.as_ref()
.expect("L1BlockInfo should be loaded")
.operator_fee_charge(gas_limit, SPEC::SPEC_ID);
.operator_fee_charge(enveloped_tx, gas_limit, SPEC::SPEC_ID);

let mut balance_check = gas_limit
.checked_mul(tx.gas_price)
Expand Down Expand Up @@ -373,7 +373,7 @@ pub fn deduct_caller<SPEC: Spec, EXT, DB: Database>(
.l1_block_info
.as_ref()
.expect("L1BlockInfo should be loaded")
.operator_fee_charge(gas_limit, SPEC::SPEC_ID);
.operator_fee_charge(enveloped_tx, gas_limit, SPEC::SPEC_ID);

caller_account.info.balance = caller_account
.info
Expand Down Expand Up @@ -413,6 +413,7 @@ pub fn reward_beneficiary<SPEC: Spec, EXT, DB: Database>(

let l1_cost = l1_block_info.calculate_tx_l1_cost(enveloped_tx, SPEC::SPEC_ID);
let operator_fee_cost = l1_block_info.operator_fee_charge(
enveloped_tx,
U256::from(gas.spent() - gas.refunded() as u64),
SPEC::SPEC_ID,
);
Expand Down
24 changes: 23 additions & 1 deletion crates/revm/src/optimism/l1block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ impl L1BlockInfo {
/// Calculate the operator fee for executing this transaction.
///
/// Introduced in isthmus. Prior to isthmus, the operator fee is always zero.
pub fn operator_fee_charge(&self, gas_limit: U256, spec_id: SpecId) -> U256 {
pub fn operator_fee_charge(&self, input: &[u8], gas_limit: U256, spec_id: SpecId) -> U256 {
// If the input is a deposit transaction or empty, the default value is zero.
if input.is_empty() || input.first() == Some(&0x7F) {
return U256::ZERO;
}
if !spec_id.is_enabled_in(SpecId::ISTHMUS) {
return U256::ZERO;
}
Expand Down Expand Up @@ -223,6 +227,7 @@ impl L1BlockInfo {
// constant.

operator_fee_scalar.saturating_mul(U256::from(gas.remaining() + gas.refunded() as u64))
/ (U256::from(OPERATOR_FEE_SCALAR_DECIMAL))
}

/// Calculate the data gas for posting the transaction on L1. Calldata costs 16 gas per byte
Expand Down Expand Up @@ -575,4 +580,21 @@ mod tests {

assert_eq!(l1_fee, expected_l1_fee)
}

#[test]
fn test_operator_fee_refund() {
let gas = Gas::new(50000);

let l1_block_info = L1BlockInfo {
l1_base_fee: U256::from(1055991687),
l1_base_fee_scalar: U256::from(5227),
operator_fee_scalar: Some(U256::from(2000)),
operator_fee_constant: Some(U256::from(5)),
..Default::default()
};

let refunded = l1_block_info.operator_fee_refund(&gas, SpecId::ISTHMUS);

assert_eq!(refunded, U256::from(100))
}
}

0 comments on commit 2c9290a

Please sign in to comment.