Skip to content

Commit

Permalink
review: Address first few comments
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Sep 5, 2023
1 parent 9bb0c49 commit a31c31c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 43 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,19 @@ impl Env {
/// Return initial spend gas (Gas needed to execute transaction).
#[inline]
pub fn validate_tx<SPEC: Spec>(&self) -> Result<(), InvalidTransaction> {
#[cfg(feature = "optimism")]
if self.cfg.optimism {
// Do not allow for a system transaction to be processed if Regolith is enabled.
if self.tx.is_system_transaction.unwrap_or(false) && SPEC::enabled(SpecId::REGOLITH) {
return Err(InvalidTransaction::DepositSystemTxPostRegolith);
}

// Do not perform any extra validation for deposit transactions, they are pre-verified on L1.
if self.tx.source_hash.is_some() {
return Ok(());
}
}

let gas_limit = self.tx.gas_limit;
let effective_gas_price = self.effective_gas_price();
let is_create = self.tx.transact_to.is_create();
Expand Down
2 changes: 0 additions & 2 deletions crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ revm-interpreter = { path = "../interpreter", version = "1.1.2", default-feature

#misc
auto_impl = { version = "1.1", default-features = false }
once_cell = "1.18.0"
bytes = "1.4.0"

# Optional
serde = { version = "1.0", features = ["derive", "rc"], optional = true }
Expand Down
27 changes: 2 additions & 25 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact<DB::Error>
fn preverify_transaction(&mut self) -> Result<(), EVMError<DB::Error>> {
let env = self.env();

#[cfg(feature = "optimism")]
let is_deposit = env.cfg.optimism && env.tx.source_hash.is_some();

env.validate_block_env::<GSPEC, DB::Error>()?;

// If the transaction is a deposit transaction on Optimism, there are no fee fields to check,
// no nonce to check, and no need to check if EOA (L1 already verified it for us)
// Gas is free, but no refunds!
#[cfg(feature = "optimism")]
if env.cfg.optimism {
if !is_deposit {
env.validate_tx::<GSPEC>()?;
}

// Do not allow for a system transaction to be processed if Regolith is enabled.
if is_deposit
&& env.tx.is_system_transaction.unwrap_or(false)
&& GSPEC::enabled(SpecId::REGOLITH)
{
return Err(InvalidTransaction::DepositSystemTxPostRegolith.into());
}
}

#[cfg(not(feature = "optimism"))]
env.validate_tx::<GSPEC>()?;

let tx_caller = env.tx.caller;
Expand Down Expand Up @@ -474,7 +451,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
let Ok((l1_fee_vault_account, _)) = self
.data
.journaled_state
.load_account(*optimism::L1_FEE_RECIPIENT, self.data.db)
.load_account(optimism::L1_FEE_RECIPIENT.into(), self.data.db)
else {
panic!("[OPTIMISM] Failed to load L1 Fee Vault account");
};
Expand All @@ -486,7 +463,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
let Ok((base_fee_vault_account, _)) = self
.data
.journaled_state
.load_account(*optimism::BASE_FEE_RECIPIENT, self.data.db)
.load_account(optimism::BASE_FEE_RECIPIENT.into(), self.data.db)
else {
panic!("[OPTIMISM] Failed to load Base Fee Vault account");
};
Expand Down
27 changes: 12 additions & 15 deletions crates/revm/src/optimism.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
//! Optimism-specific constants, types, and helpers.

use bytes::Bytes;
use core::ops::Mul;
use once_cell::sync::Lazy;
use revm_interpreter::primitives::{db::Database, hex_literal::hex, Address, Spec, SpecId, U256};
use revm_interpreter::primitives::{
db::Database, hex_literal::hex, Bytes, Spec, SpecId, B160, U256,
};

const ZERO_BYTE_COST: u64 = 4;
const NON_ZERO_BYTE_COST: u64 = 16;

static L1_BASE_FEE_SLOT: Lazy<U256> = Lazy::new(|| U256::from(1));
static L1_OVERHEAD_SLOT: Lazy<U256> = Lazy::new(|| U256::from(5));
static L1_SCALAR_SLOT: Lazy<U256> = Lazy::new(|| U256::from(6));
const L1_BASE_FEE_SLOT: U256 = U256::from_limbs([1u64, 0, 0, 0]);
const L1_OVERHEAD_SLOT: U256 = U256::from_limbs([5u64, 0, 0, 0]);
const L1_SCALAR_SLOT: U256 = U256::from_limbs([6u64, 0, 0, 0]);

/// The address of L1 fee recipient.
pub static L1_FEE_RECIPIENT: Lazy<Address> =
Lazy::new(|| Address::from_slice(&hex!("420000000000000000000000000000000000001A")));
pub const L1_FEE_RECIPIENT: B160 = B160(hex!("420000000000000000000000000000000000001A"));

/// The address of the base fee recipient.
pub static BASE_FEE_RECIPIENT: Lazy<Address> =
Lazy::new(|| Address::from_slice(&hex!("4200000000000000000000000000000000000019")));
pub const BASE_FEE_RECIPIENT: B160 = B160(hex!("4200000000000000000000000000000000000019"));

/// The address of the L1Block contract.
pub static L1_BLOCK_CONTRACT: Lazy<Address> =
Lazy::new(|| Address::from_slice(&hex!("4200000000000000000000000000000000000015")));
pub const L1_BLOCK_CONTRACT: B160 = B160(hex!("4200000000000000000000000000000000000015"));

/// L1 block info
///
Expand All @@ -48,9 +45,9 @@ pub struct L1BlockInfo {
impl L1BlockInfo {
/// Fetches the L1 block info from the `L1Block` contract in the database.
pub fn try_fetch<DB: Database>(db: &mut DB) -> Result<L1BlockInfo, DB::Error> {
let l1_base_fee = db.storage(*L1_BLOCK_CONTRACT, *L1_BASE_FEE_SLOT)?;
let l1_fee_overhead = db.storage(*L1_BLOCK_CONTRACT, *L1_OVERHEAD_SLOT)?;
let l1_fee_scalar = db.storage(*L1_BLOCK_CONTRACT, *L1_SCALAR_SLOT)?;
let l1_base_fee = db.storage(L1_BLOCK_CONTRACT.into(), L1_BASE_FEE_SLOT)?;
let l1_fee_overhead = db.storage(L1_BLOCK_CONTRACT.into(), L1_OVERHEAD_SLOT)?;
let l1_fee_scalar = db.storage(L1_BLOCK_CONTRACT.into(), L1_SCALAR_SLOT)?;

Ok(L1BlockInfo {
l1_base_fee,
Expand Down

0 comments on commit a31c31c

Please sign in to comment.