Skip to content

Commit

Permalink
refactor: make ChainSpec associated type on EvmWiring
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Sep 19, 2024
1 parent 66adad0 commit ccc9755
Show file tree
Hide file tree
Showing 21 changed files with 240 additions and 128 deletions.
7 changes: 4 additions & 3 deletions crates/inspector/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ mod tests {
bytecode::Bytecode,
interpreter::{opcode, Interpreter},
primitives::{address, Bytes, Log, TxKind},
wiring::EvmWiring as PrimitiveEvmWiring,
wiring::{DefaultEthereumWiring, EthereumWiring},
wiring::{
ChainSpec, DefaultEthereumWiring, EthereumWiring, EvmWiring as PrimitiveEvmWiring,
},
Evm, EvmWiring,
};

Expand Down Expand Up @@ -181,7 +182,7 @@ mod tests {
.with_db(BenchmarkDB::new_bytecode(bytecode.clone()))
.with_default_ext_ctx()
.modify_tx_env(|tx| {
*tx = <TestEvmWiring as PrimitiveEvmWiring>::Transaction::default();
*tx = <<TestEvmWiring as PrimitiveEvmWiring>::ChainSpec as ChainSpec>::Transaction::default();

tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
Expand Down
6 changes: 4 additions & 2 deletions crates/inspector/src/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ mod tests {
database_interface::EmptyDB,
interpreter::{opcode, CallInputs, CallOutcome, CreateInputs, CreateOutcome},
primitives::{address, Bytes, TxKind},
wiring::{DefaultEthereumWiring, EthereumWiring, EvmWiring as PrimitiveEvmWiring},
wiring::{
ChainSpec, DefaultEthereumWiring, EthereumWiring, EvmWiring as PrimitiveEvmWiring,
},
Evm, EvmContext, EvmWiring,
};

Expand Down Expand Up @@ -374,7 +376,7 @@ mod tests {
.with_db(BenchmarkDB::new_bytecode(bytecode.clone()))
.with_external_context(StackInspector::default())
.modify_tx_env(|tx| {
*tx = <TestEvmWiring as PrimitiveEvmWiring>::Transaction::default();
*tx = <<TestEvmWiring as PrimitiveEvmWiring>::ChainSpec as ChainSpec>::Transaction::default();

tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
Expand Down
11 changes: 4 additions & 7 deletions crates/interpreter/src/host/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ use crate::{Host, SStoreResult, SelfDestructResult};
use derive_where::derive_where;
use primitives::{hash_map::Entry, Address, Bytes, HashMap, Log, B256, KECCAK_EMPTY, U256};
use std::vec::Vec;
use wiring::{
default::{Env, EnvWiring},
EvmWiring,
};
use wiring::{default::EnvWiring, ChainSpec, EvmWiring};

use super::{AccountLoad, Eip7702CodeLoad, StateLoad};

/// A dummy [Host] implementation.
#[derive_where(Clone, Debug, Default; EvmWiringT::Block, EvmWiringT::Transaction)]
#[derive_where(Clone, Debug, Default; <EvmWiringT::ChainSpec as ChainSpec>::Block, <EvmWiringT::ChainSpec as ChainSpec>::Transaction)]
pub struct DummyHost<EvmWiringT>
where
EvmWiringT: EvmWiring,
{
pub env: Env<EvmWiringT::Block, EvmWiringT::Transaction>,
pub env: EnvWiring<EvmWiringT>,
pub storage: HashMap<U256, U256>,
pub transient_storage: HashMap<U256, U256>,
pub log: Vec<Log>,
Expand All @@ -25,7 +22,7 @@ impl<EvmWiringT> DummyHost<EvmWiringT>
where
EvmWiringT: EvmWiring,
{
/// Create a new dummy host with the given [`Env`].
/// Create a new dummy host with the given [`wiring::default::Env`].
#[inline]
pub fn new(env: EnvWiring<EvmWiringT>) -> Self {
Self {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bytecode::Eof;
use primitives::{Address, Bytes, U256};
use wiring::{EvmWiring, Transaction};
use wiring::{ChainSpec, EvmWiring, Transaction};

/// EOF create can be called from two places:
/// * EOFCREATE opcode
Expand Down Expand Up @@ -75,7 +75,10 @@ impl EOFCreateInputs {
}

/// Creates new EOFCreateInputs from transaction.
pub fn new_tx<EvmWiringT: EvmWiring>(tx: &EvmWiringT::Transaction, gas_limit: u64) -> Self {
pub fn new_tx<EvmWiringT: EvmWiring>(
tx: &<EvmWiringT::ChainSpec as ChainSpec>::Transaction,
gas_limit: u64,
) -> Self {
EOFCreateInputs::new(
*tx.caller(),
*tx.value(),
Expand Down
18 changes: 13 additions & 5 deletions crates/optimism/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use l1block::{L1BlockInfo, BASE_FEE_RECIPIENT, L1_BLOCK_CONTRACT, L1_FEE_REC
pub use result::{OptimismHaltReason, OptimismInvalidTransaction};
use revm::{
primitives::{Bytes, B256},
wiring::TransactionValidation,
wiring::{ChainSpec, TransactionValidation},
};
pub use spec::*;

Expand Down Expand Up @@ -63,8 +63,8 @@ pub trait OptimismTransaction {
}

/// Trait for an Optimism chain spec.
pub trait OptimismWiring:
revm::EvmWiring<
pub trait OptimismChainSpec:
ChainSpec<
ChainContext: OptimismContext,
Hardfork = OptimismSpecId,
HaltReason = OptimismHaltReason,
Expand All @@ -74,8 +74,8 @@ pub trait OptimismWiring:
{
}

impl<EvmWiringT> OptimismWiring for EvmWiringT where
EvmWiringT: revm::EvmWiring<
impl<ChainSpecT> OptimismChainSpec for ChainSpecT where
ChainSpecT: ChainSpec<
ChainContext: OptimismContext,
Hardfork = OptimismSpecId,
HaltReason = OptimismHaltReason,
Expand All @@ -84,3 +84,11 @@ impl<EvmWiringT> OptimismWiring for EvmWiringT where
>
{
}

/// Trait for Optimism `EvmWiring`.
pub trait OptimismWiring: revm::EvmWiring<ChainSpec: OptimismChainSpec> {}

impl<EvmWiringT> OptimismWiring for EvmWiringT where
EvmWiringT: revm::EvmWiring<ChainSpec: OptimismChainSpec>
{
}
23 changes: 15 additions & 8 deletions crates/optimism/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,34 @@ use revm::{
precompile::PrecompileSpecId,
specification::hardfork::{Spec, SpecId},
wiring::default::block::BlockEnv,
wiring::EvmWiring,
wiring::{ChainSpec, EvmWiring},
EvmHandler,
};

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct OptimismChainSpec;

impl ChainSpec for OptimismChainSpec {
type Block = BlockEnv;
type ChainContext = Context;
type Transaction = TxEnv;
type Hardfork = OptimismSpecId;
type HaltReason = OptimismHaltReason;
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct OptimismEvmWiring<DB: Database, EXT> {
_phantom: PhantomData<(DB, EXT)>,
phantom: PhantomData<(DB, EXT)>,
}

impl<DB: Database, EXT> EvmWiring for OptimismEvmWiring<DB, EXT> {
type Block = BlockEnv;
type ChainSpec = OptimismChainSpec;
type Database = DB;
type ChainContext = Context;
type ExternalContext = EXT;
type Hardfork = OptimismSpecId;
type HaltReason = OptimismHaltReason;
type Transaction = TxEnv;
}

impl<DB: Database, EXT> revm::EvmWiring for OptimismEvmWiring<DB, EXT> {
fn handler<'evm>(hardfork: Self::Hardfork) -> EvmHandler<'evm, Self>
fn handler<'evm>(hardfork: <Self::ChainSpec as ChainSpec>::Hardfork) -> EvmHandler<'evm, Self>
where
DB: Database,
{
Expand Down
Loading

0 comments on commit ccc9755

Please sign in to comment.