Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove OpSpec #2074

Merged
merged 5 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions crates/optimism/src/api/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{evm::OpEvm, transaction::OpTxTrait, L1BlockInfo, OpSpec, OpTransaction};
use crate::{evm::OpEvm, transaction::OpTxTrait, L1BlockInfo, OpSpecId, OpTransaction};
use precompile::Log;
use revm::{
context::{BlockEnv, Cfg, CfgEnv, TxEnv},
Expand Down Expand Up @@ -27,7 +27,7 @@ impl<BLOCK, TX, CFG, DB, JOURNAL> OpBuilder for Context<BLOCK, TX, CFG, DB, JOUR
where
BLOCK: Block,
TX: OpTxTrait,
CFG: Cfg<Spec = OpSpec>,
CFG: Cfg<Spec = OpSpecId>,
DB: Database,
JOURNAL: Journal<Database = DB, FinalOutput = (EvmState, Vec<Log>)>,
{
Expand All @@ -48,4 +48,4 @@ where
}

pub type OpContext<DB> =
Context<BlockEnv, OpTransaction<TxEnv>, CfgEnv<OpSpec>, DB, JournaledState<DB>, L1BlockInfo>;
Context<BlockEnv, OpTransaction<TxEnv>, CfgEnv<OpSpecId>, DB, JournaledState<DB>, L1BlockInfo>;
8 changes: 4 additions & 4 deletions crates/optimism/src/api/default_ctx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{L1BlockInfo, OpSpec, OpSpecId, OpTransaction};
use crate::{L1BlockInfo, OpSpecId, OpTransaction};
use revm::{
context::{BlockEnv, CfgEnv, TxEnv},
database_interface::EmptyDB,
Expand All @@ -9,7 +9,7 @@ pub trait DefaultOp {
fn op() -> Context<
BlockEnv,
OpTransaction<TxEnv>,
CfgEnv<OpSpec>,
CfgEnv<OpSpecId>,
EmptyDB,
JournaledState<EmptyDB>,
L1BlockInfo,
Expand All @@ -20,7 +20,7 @@ impl DefaultOp
for Context<
BlockEnv,
OpTransaction<TxEnv>,
CfgEnv<OpSpec>,
CfgEnv<OpSpecId>,
EmptyDB,
JournaledState<EmptyDB>,
L1BlockInfo,
Expand All @@ -29,7 +29,7 @@ impl DefaultOp
fn op() -> Self {
Context::mainnet()
.with_tx(OpTransaction::default())
.with_cfg(CfgEnv::new().with_spec(OpSpec::Op(OpSpecId::BEDROCK)))
.with_cfg(CfgEnv::new().with_spec(OpSpecId::BEDROCK))
.with_chain(L1BlockInfo::default())
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/optimism/src/api/exec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
evm::OpEvm, handler::OpHandler, transaction::OpTxTrait, L1BlockInfo, OpHaltReason, OpSpec,
evm::OpEvm, handler::OpHandler, transaction::OpTxTrait, L1BlockInfo, OpHaltReason, OpSpecId,
OpTransactionError,
};
use precompile::Log;
Expand Down Expand Up @@ -29,7 +29,7 @@ impl<BLOCK, TX, CFG, DB, JOURNAL, INSP> ExecuteEvm
where
BLOCK: Block,
TX: OpTxTrait,
CFG: Cfg<Spec = OpSpec>,
CFG: Cfg<Spec = OpSpecId>,
DB: Database,
JOURNAL: Journal<Database = DB, FinalOutput = (EvmState, Vec<Log>)> + JournalExt,
INSP: Inspector<Context<BLOCK, TX, CFG, DB, JOURNAL, L1BlockInfo>, EthInterpreter>,
Expand All @@ -52,7 +52,7 @@ impl<BLOCK, TX, CFG, DB, JOURNAL, INSP> ExecuteCommitEvm
where
BLOCK: Block,
TX: OpTxTrait,
CFG: Cfg<Spec = OpSpec>,
CFG: Cfg<Spec = OpSpecId>,
DB: Database + DatabaseCommit,
JOURNAL: Journal<Database = DB, FinalOutput = (EvmState, Vec<Log>)> + JournalExt,
INSP: Inspector<Context<BLOCK, TX, CFG, DB, JOURNAL, L1BlockInfo>, EthInterpreter>,
Expand All @@ -79,7 +79,7 @@ impl<BLOCK, TX, CFG, DB, JOURNAL, INSP> InspectEvm
where
BLOCK: Block,
TX: OpTxTrait,
CFG: Cfg<Spec = OpSpec>,
CFG: Cfg<Spec = OpSpecId>,
DB: Database,
JOURNAL: Journal<Database = DB, FinalOutput = (EvmState, Vec<Log>)> + JournalExt,
INSP: Inspector<Context<BLOCK, TX, CFG, DB, JOURNAL, L1BlockInfo>, EthInterpreter>,
Expand All @@ -105,7 +105,7 @@ impl<BLOCK, TX, CFG, DB, JOURNAL, INSP> InspectCommitEvm
where
BLOCK: Block,
TX: OpTxTrait,
CFG: Cfg<Spec = OpSpec>,
CFG: Cfg<Spec = OpSpecId>,
DB: Database + DatabaseCommit,
JOURNAL: Journal<Database = DB, FinalOutput = (EvmState, Vec<Log>)> + JournalExt,
INSP: Inspector<Context<BLOCK, TX, CFG, DB, JOURNAL, L1BlockInfo>, EthInterpreter>,
Expand Down
16 changes: 10 additions & 6 deletions crates/optimism/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
deposit::{DepositTransaction, DEPOSIT_TRANSACTION_TYPE},
OpTransactionError, OpTxTrait,
},
L1BlockInfo, OpHaltReason, OpSpec, OpSpecId,
L1BlockInfo, OpHaltReason, OpSpecId,
};
use precompile::Log;
use revm::{
Expand Down Expand Up @@ -66,7 +66,7 @@ where
Context: ContextTrait<
Journal: Journal<FinalOutput = (EvmState, Vec<Log>)>,
Tx: OpTxTrait,
Cfg: Cfg<Spec = OpSpec>,
Cfg: Cfg<Spec = OpSpecId>,
Chain = L1BlockInfo,
>,
>,
Expand Down Expand Up @@ -290,9 +290,13 @@ where
// Prior to Regolith, deposit transactions did not receive gas refunds.
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().is_enabled_in(SpecId::LONDON));
exec_result.gas_mut().set_final_refund(
evm.ctx()
.cfg()
.spec()
.into_eth_spec()
.is_enabled_in(SpecId::LONDON),
);
}
}

Expand Down Expand Up @@ -444,7 +448,7 @@ where
Context: ContextTrait<
Journal: Journal<FinalOutput = (EvmState, Vec<Log>)>,
Tx: OpTxTrait,
Cfg: Cfg<Spec = OpSpec>,
Cfg: Cfg<Spec = OpSpecId>,
Chain = L1BlockInfo,
>,
Inspector: Inspector<<<Self as EthHandler>::Evm as EvmTrait>::Context, EthInterpreter>,
Expand Down
43 changes: 11 additions & 32 deletions crates/optimism/src/handler/precompiles.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::{OpSpec, OpSpecId};
use crate::OpSpecId;
use once_cell::race::OnceBox;
use precompile::{secp256r1, PrecompileErrors, Precompiles};
use revm::{
context::Cfg,
context_interface::ContextTrait,
handler::{EthPrecompiles, PrecompileProvider},
interpreter::InterpreterResult,
specification::hardfork::SpecId,
};
use std::boxed::Box;

Expand All @@ -33,35 +32,15 @@ impl<CTX> OpPrecompileProvider<CTX> {
}

#[inline]
pub fn new_with_spec(spec: OpSpec) -> Self {
pub fn new_with_spec(spec: OpSpecId) -> Self {
match spec {
spec @ (OpSpec::Eth(
SpecId::FRONTIER
| SpecId::FRONTIER_THAWING
| SpecId::HOMESTEAD
| SpecId::DAO_FORK
| SpecId::TANGERINE
| SpecId::SPURIOUS_DRAGON
| SpecId::BYZANTIUM
| SpecId::CONSTANTINOPLE
| SpecId::PETERSBURG
| SpecId::ISTANBUL
| SpecId::MUIR_GLACIER
| SpecId::BERLIN
| SpecId::LONDON
| SpecId::ARROW_GLACIER
| SpecId::GRAY_GLACIER
| SpecId::MERGE
| SpecId::SHANGHAI
| SpecId::CANCUN,
)
| OpSpec::Op(
OpSpecId::BEDROCK | OpSpecId::REGOLITH | OpSpecId::CANYON | OpSpecId::ECOTONE,
)) => Self::new(Precompiles::new(spec.into_eth_spec().into())),
OpSpec::Op(OpSpecId::FJORD) => Self::new(fjord()),
OpSpec::Op(OpSpecId::GRANITE | OpSpecId::HOLOCENE) => Self::new(granite()),
OpSpec::Op(OpSpecId::ISTHMUS)
| OpSpec::Eth(SpecId::PRAGUE | SpecId::OSAKA | SpecId::LATEST) => Self::new(isthumus()),
spec @ (OpSpecId::BEDROCK
| OpSpecId::REGOLITH
| OpSpecId::CANYON
| OpSpecId::ECOTONE) => Self::new(Precompiles::new(spec.into_eth_spec().into())),
OpSpecId::FJORD => Self::new(fjord()),
OpSpecId::GRANITE | OpSpecId::HOLOCENE => Self::new(granite()),
OpSpecId::ISTHMUS => Self::new(isthumus()),
}
}
}
Expand Down Expand Up @@ -107,7 +86,7 @@ pub fn isthumus() -> &'static Precompiles {

impl<CTX> PrecompileProvider for OpPrecompileProvider<CTX>
where
CTX: ContextTrait<Cfg: Cfg<Spec = OpSpec>>,
CTX: ContextTrait<Cfg: Cfg<Spec = OpSpecId>>,
{
type Context = CTX;
type Output = InterpreterResult;
Expand Down Expand Up @@ -142,6 +121,6 @@ where

impl<CTX> Default for OpPrecompileProvider<CTX> {
fn default() -> Self {
Self::new_with_spec(OpSpec::Op(OpSpecId::ISTHMUS))
Self::new_with_spec(OpSpecId::ISTHMUS)
}
}
21 changes: 12 additions & 9 deletions crates/optimism/src/l1block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
ZERO_BYTE_COST,
},
transaction::estimate_tx_compressed_size,
OpSpec, OpSpecId,
OpSpecId,
};
use core::ops::Mul;
use revm::{
Expand Down Expand Up @@ -48,10 +48,13 @@ pub struct L1BlockInfo {

impl L1BlockInfo {
/// Try to fetch the L1 block info from the database.
pub fn try_fetch<DB: Database>(db: &mut DB, spec_id: OpSpec) -> Result<L1BlockInfo, DB::Error> {
pub fn try_fetch<DB: Database>(
db: &mut DB,
spec_id: OpSpecId,
) -> Result<L1BlockInfo, DB::Error> {
// Ensure the L1 Block account is loaded into the cache after Ecotone. With EIP-4788, it is no longer the case
// that the L1 block account is loaded into the cache prior to the first inquiry for the L1 block info.
if spec_id.is_enabled_in(SpecId::CANCUN) {
if spec_id.into_eth_spec().is_enabled_in(SpecId::CANCUN) {
let _ = db.basic(L1_BLOCK_CONTRACT)?;
}

Expand Down Expand Up @@ -138,7 +141,7 @@ 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, input: &[u8], gas_limit: U256, spec_id: OpSpec) -> U256 {
pub fn operator_fee_charge(&self, input: &[u8], gas_limit: U256, spec_id: OpSpecId) -> 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;
Expand All @@ -162,7 +165,7 @@ 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_refund(&self, gas: &Gas, spec_id: OpSpec) -> U256 {
pub fn operator_fee_refund(&self, gas: &Gas, spec_id: OpSpecId) -> U256 {
if !spec_id.is_enabled_in(OpSpecId::ISTHMUS) {
return U256::ZERO;
}
Expand All @@ -185,7 +188,7 @@ impl L1BlockInfo {
///
/// Prior to regolith, an extra 68 non-zero bytes were included in the rollup data costs to
/// account for the empty signature.
pub fn data_gas(&self, input: &[u8], spec_id: OpSpec) -> U256 {
pub fn data_gas(&self, input: &[u8], spec_id: OpSpecId) -> U256 {
if spec_id.is_enabled_in(OpSpecId::FJORD) {
let estimated_size = self.tx_estimated_size_fjord(input);

Expand Down Expand Up @@ -218,7 +221,7 @@ impl L1BlockInfo {
}

/// Calculate the gas cost of a transaction based on L1 block data posted on L2, depending on the [OpSpec] passed.
pub fn calculate_tx_l1_cost(&self, input: &[u8], spec_id: OpSpec) -> U256 {
pub fn calculate_tx_l1_cost(&self, input: &[u8], spec_id: OpSpecId) -> 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;
Expand All @@ -234,7 +237,7 @@ impl L1BlockInfo {
}

/// Calculate the gas cost of a transaction based on L1 block data posted on L2, pre-Ecotone.
fn calculate_tx_l1_cost_bedrock(&self, input: &[u8], spec_id: OpSpec) -> U256 {
fn calculate_tx_l1_cost_bedrock(&self, input: &[u8], spec_id: OpSpecId) -> U256 {
let rollup_data_gas_cost = self.data_gas(input, spec_id);
rollup_data_gas_cost
.saturating_add(self.l1_fee_overhead.unwrap_or_default())
Expand All @@ -253,7 +256,7 @@ impl L1BlockInfo {
///
/// Function is actually computed as follows for better precision under integer arithmetic:
/// `calldataGas*(l1BaseFee*16*l1BaseFeeScalar + l1BlobBaseFee*l1BlobBaseFeeScalar)/16e6`
fn calculate_tx_l1_cost_ecotone(&self, input: &[u8], spec_id: OpSpec) -> U256 {
fn calculate_tx_l1_cost_ecotone(&self, input: &[u8], spec_id: OpSpecId) -> U256 {
// There is an edgecase where, for the very first Ecotone block (unless it is activated at Genesis), we must
// use the Bedrock cost function. To determine if this is the case, we can check if the Ecotone parameters are
// unset.
Expand Down
Loading
Loading