Skip to content

Commit

Permalink
skip if basefee is zero, like geth.
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Sep 16, 2024
1 parent 6155ebc commit 3ce57aa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
44 changes: 21 additions & 23 deletions crates/optimism/src/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,12 @@ pub fn reward_beneficiary<EvmWiringT: OptimismWiring, SPEC: OptimismSpec>(
) -> EVMResultGeneric<(), EvmWiringT> {
let is_deposit = context.evm.inner.env.tx.source_hash().is_some();

// transfer fee to coinbase/beneficiary if the transaction is not a deposit.
// transfer fee to coinbase/beneficiary.
if !is_deposit {
mainnet::reward_beneficiary::<EvmWiringT, SPEC>(context, gas)?;
}

if !is_deposit {
// If the transaction is not a deposit transaction, fees are paid out
// to both the Base Fee Vault as well as the L1 Fee Vault.
let l1_block_info = context
Expand All @@ -297,36 +299,32 @@ pub fn reward_beneficiary<EvmWiringT: OptimismWiring, SPEC: OptimismSpec>(
};

let l1_cost = l1_block_info.calculate_tx_l1_cost(enveloped_tx, SPEC::OPTIMISM_SPEC_ID);
if l1_cost.gt(&U256::ZERO) {
// Send the L1 cost of the transaction to the L1 Fee Vault.
let mut l1_fee_vault_account = context
.evm
.inner
.journaled_state
.load_account(L1_FEE_RECIPIENT, &mut context.evm.inner.db)
.map_err(EVMError::Database)?;
l1_fee_vault_account.mark_touch();
l1_fee_vault_account.info.balance += l1_cost;
}

// Send the L1 cost of the transaction to the L1 Fee Vault.
let mut l1_fee_vault_account = context
.evm
.inner
.journaled_state
.load_account(L1_FEE_RECIPIENT, &mut context.evm.inner.db)
.map_err(EVMError::Database)?;
l1_fee_vault_account.mark_touch();
l1_fee_vault_account.info.balance += l1_cost;

// Send the base fee of the transaction to the Base Fee Vault.
let reward = context
let mut base_fee_vault_account = context
.evm
.inner
.journaled_state
.load_account(BASE_FEE_RECIPIENT, &mut context.evm.inner.db)
.map_err(EVMError::Database)?;
base_fee_vault_account.mark_touch();
base_fee_vault_account.info.balance += context
.evm
.inner
.env
.block
.basefee()
.mul(U256::from(gas.spent() - gas.refunded() as u64));
if reward.gt(&U256::ZERO) {
let mut base_fee_vault_account = context
.evm
.inner
.journaled_state
.load_account(BASE_FEE_RECIPIENT, &mut context.evm.inner.db)
.map_err(EVMError::Database)?;
base_fee_vault_account.mark_touch();
base_fee_vault_account.info.balance += reward;
}
}
Ok(())
}
Expand Down
38 changes: 25 additions & 13 deletions crates/revm/src/handler/mainnet/post_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,36 @@ pub fn reward_beneficiary<EvmWiringT: EvmWiring, SPEC: Spec>(
// transfer fee to coinbase/beneficiary.
// EIP-1559 discard basefee for coinbase transfer. Basefee amount of gas is discarded.
let coinbase_gas_price = if SPEC::enabled(LONDON) {
effective_gas_price.saturating_sub(*context.evm.env.block.basefee())
let basefee = *context.evm.env.block.basefee();

// Skip the transfer if basefee and max priority fee per gas is zero, like geth.
if basefee.is_zero()
&& context
.evm
.env
.tx
.max_priority_fee_per_gas()
.unwrap_or(&U256::ZERO)
.is_zero()
{
return Ok(());
}

effective_gas_price.saturating_sub(basefee)
} else {
effective_gas_price
};

let reward = coinbase_gas_price * U256::from(gas.spent() - gas.refunded() as u64);
if reward.gt(&U256::ZERO) {
let coinbase_account = context
.evm
.inner
.journaled_state
.load_account(beneficiary, &mut context.evm.inner.db)
.map_err(EVMError::Database)?;

coinbase_account.data.mark_touch();
coinbase_account.data.info.balance =
coinbase_account.data.info.balance.saturating_add(reward);
}
let coinbase_account = context
.evm
.inner
.journaled_state
.load_account(beneficiary, &mut context.evm.inner.db)
.map_err(EVMError::Database)?;

coinbase_account.data.mark_touch();
coinbase_account.data.info.balance = coinbase_account.data.info.balance.saturating_add(reward);

Ok(())
}
Expand Down

0 comments on commit 3ce57aa

Please sign in to comment.