Skip to content
This repository was archived by the owner on Oct 11, 2024. It is now read-only.

fix: Refactor l1 cost and mint computations #4

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 73 additions & 25 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,65 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
}
Ok(())
}

/// If the transaction is not a deposit transaction, subtract the L1 data fee from the
/// caller's balance directly after minting the requested amount of ETH.
#[cfg(feature = "optimism")]
fn remove_l1_cost(
is_deposit: bool,
tx_caller: B160,
tx_l1_cost: Option<U256>,
db: &mut DB,
journal: &mut JournaledState,
) -> Result<(), EVMError<DB::Error>> {
if is_deposit {
return Ok(());
}
if let Some(l1_cost) = tx_l1_cost {
let acc = journal
.load_account(tx_caller, db)
.map_err(EVMError::Database)?
.0;
if l1_cost.gt(&acc.info.balance) {
let x = l1_cost.as_limbs();
let u64_cost = if x[1] == 0 && x[2] == 0 && x[3] == 0 {
x[0]
} else {
u64::MAX
};
return Err(EVMError::Transaction(
InvalidTransaction::LackOfFundForMaxFee {
fee: u64_cost,
balance: acc.info.balance,
},
));
}
acc.info.balance = acc.info.balance.saturating_sub(l1_cost);
}
Ok(())
}

/// If the transaction is a deposit with a `mint` value, add the mint value
/// in wei to the caller's balance. This should be persisted to the database
/// prior to the rest of execution.
#[cfg(feature = "optimism")]
fn commit_mint_value(
tx_caller: B160,
tx_mint: Option<u128>,
db: &mut DB,
journal: &mut JournaledState,
) -> Result<(), EVMError<DB::Error>> {
if let Some(mint) = tx_mint {
journal
.load_account(tx_caller, db)
.map_err(EVMError::Database)?
.0
.info
.balance += U256::from(mint);
journal.checkpoint();
}
Ok(())
}
}

impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact<DB::Error>
Expand Down Expand Up @@ -158,31 +217,20 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact<DB::Error>

#[cfg(feature = "optimism")]
if self.data.env.cfg.optimism {
// If the transaction is a deposit with a `Some` `mint` value, add the minted value
// in wei to the caller's balance. This should be persisted to the database prior
// to the rest of execution below.
if let Some(mint) = tx_mint {
journal
.load_account(tx_caller, self.data.db)
.map_err(EVMError::Database)?
.0
.info
.balance += U256::from(mint);
journal.checkpoint();
}

// If the transaction is not a deposit transaction, subtract the L1 data fee from the
// caller's balance directly after minting the requested amount of ETH.
if !is_deposit {
if let Some(l1_cost) = tx_l1_cost {
journal
.load_account(tx_caller, self.data.db)
.map_err(EVMError::Database)?
.0
.info
.balance -= l1_cost;
}
}
EVMImpl::<GSPEC, DB, INSPECT>::commit_mint_value(
tx_caller,
tx_mint,
self.data.db,
journal,
)?;

EVMImpl::<GSPEC, DB, INSPECT>::remove_l1_cost(
is_deposit,
tx_caller,
tx_l1_cost,
self.data.db,
journal,
)?;
}

let (caller_account, _) = journal
Expand Down