Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
f8fcaa7
refactor: allow chain-specific configuration of Evm
Wodann May 1, 2024
6145d39
refactor: rename Transaction::transact_to and clarify docs
Wodann Jun 18, 2024
7b950c7
refactor: remove trait bounds on Transaction super trait
Wodann Jun 18, 2024
caaf05d
refactor: remove trait bounds from Block supertrait
Wodann Jun 18, 2024
72272c9
fix: clippy warnings
Wodann Jun 19, 2024
0723ff0
fix: cargo doc
Wodann Jun 19, 2024
f5ccf54
refactor: limit trait bounds on HaltReason
Wodann Jun 19, 2024
6a881fa
refactor: allow moving of kind
Wodann Jun 19, 2024
dab9e20
refactor: rename Transaction::nonce to nonce_opt to signal that it's …
Wodann Jun 19, 2024
214c1d5
refactor: replace AccessList with alloy version
Wodann Jun 19, 2024
7303587
refactor: rename gas_priority_fee to max_priority_fee_per_gas
Wodann Jun 20, 2024
a8227c0
refactor: correct trait bound on ExecutionResult::clone
Wodann Jun 20, 2024
fe1ab43
Clone
Wodann Jun 20, 2024
373e197
refactor: only allow optional nonce check via CfgEnv
Wodann Jun 20, 2024
689be7f
fix: revme
Wodann Jun 24, 2024
f53a2a9
refactor: derive DummyHost
Wodann Jun 24, 2024
dbe9e3a
refactor: derive Clone for ExecutionResult
Wodann Jun 24, 2024
77e19ce
refactor: remove EVMErrorForChain
Wodann Jun 24, 2024
6c16bef
refactor: derive Clone for CfgEnvWithChainSpec
Wodann Jun 24, 2024
145a4ce
refactor: use EVMResultGeneric
Wodann Jun 24, 2024
3f29a23
refactor: add convenience EVMErrorForChain type alias
Wodann Jun 24, 2024
3c6c4d3
feat: export OptimismBlock
Wodann Jul 8, 2024
4856bea
refactor: add handler constructor and Context to revm::ChainSpec
Wodann Jul 22, 2024
93089c3
refactor: generalise optimism implementation using traits
Wodann Jul 22, 2024
da8175a
fix: no-default-features
Wodann Jul 23, 2024
0f052b5
fix: CI
Wodann Jul 29, 2024
69e7304
chore: Add default fn to Tx/Block traits
rakita Jul 31, 2024
946d404
Chore: rename ChainSpec to EvmWiring
rakita Jul 31, 2024
57a3feb
chore: clippy comments fix
rakita Jul 31, 2024
b6e90b6
chore: rename EthEvmWiring to EthereumWiring
rakita Jul 31, 2024
69890ae
chore: re add serde, restring HaltReasonTrait
rakita Jul 31, 2024
9a6d2e6
chore: move custom opcode to examples
rakita Jul 31, 2024
da81d97
chore: remove op feature from test wiring
rakita Jul 31, 2024
1ad97fd
nit use Self::EvmWiringT
rakita Jul 31, 2024
6d479da
nit indents
rakita Jul 31, 2024
1bce037
feat(Wiring): Add Database and EXT to EvmWiring
rakita Aug 1, 2024
1c660da
some fixes
rakita Aug 1, 2024
ed99ce9
temp
rakita Aug 5, 2024
45c07c5
feat: make builder compile. EnvWiring and Result Halt
rakita Aug 5, 2024
d73f96c
chore: cleanup rename
rakita Aug 6, 2024
763ba26
nit
rakita Aug 6, 2024
b96b397
fix: make string conversion complete
Wodann Aug 28, 2024
62b45b7
Merge remote-tracking branch 'origin/main' into wiring
rakita Sep 2, 2024
65d4f26
fix compile
rakita Sep 2, 2024
83a0803
Merge remote-tracking branch 'wodann/refactor/opt-spec-id' into wiring
rakita Sep 2, 2024
917a753
compiles
rakita Sep 4, 2024
28ac623
Merge remote-tracking branch 'origin/main' into wiring
rakita Sep 4, 2024
897f8ed
wip builder
rakita Sep 4, 2024
6528d3b
wip
rakita Sep 4, 2024
321972c
Merge remote-tracking branch 'origin/main' into wiring
rakita Sep 5, 2024
0462c5b
fix compile
rakita Sep 5, 2024
c1c57ab
wip
rakita Sep 5, 2024
c4845c2
fix optimism test
rakita Sep 5, 2024
de4e89c
fix docs ci
rakita Sep 5, 2024
492c27a
cleanup
rakita Sep 6, 2024
f1faa47
cleanup
rakita Sep 6, 2024
f97ce47
use core::error::Error
rakita Sep 6, 2024
d64f624
cleanup
rakita Sep 6, 2024
4a7e6e3
use core error
rakita Sep 6, 2024
d51cc59
fix builer
rakita Sep 6, 2024
e7d78ca
fix docs
rakita Sep 6, 2024
763592a
final doc fix
rakita Sep 6, 2024
5fef8e2
rm alloy provider
rakita Sep 6, 2024
1383df7
Merge remote-tracking branch 'origin/main' into wiring
rakita Sep 9, 2024
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
Prev Previous commit
Next Next commit
refactor: add handler constructor and Context to revm::ChainSpec
  • Loading branch information
Wodann committed Jul 22, 2024
commit 4856bea758dbd2d154b29857f2c56a3e045e7c6b
98 changes: 22 additions & 76 deletions crates/revm/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::{
db::{Database, DatabaseRef, EmptyDB, WrapDatabaseRef},
handler::{
register::{self, EvmHandler},
CfgEnvWithChainSpec, EnvWithChainSpec,
},
primitives::{CfgEnv, ChainSpec, Env, EthChainSpec, InvalidTransaction, TransactionValidation},
Context, ContextWithChainSpec, Evm, EvmContext, Handler,
handler::{register, CfgEnvWithChainSpec, EnvWithChainSpec},
primitives::{self, CfgEnv, Env, EthChainSpec, InvalidTransaction, TransactionValidation},
ChainSpec, Context, ContextWithChainSpec, Evm, EvmContext, Handler,
};
use core::marker::PhantomData;
use std::boxed::Box;
Expand Down Expand Up @@ -33,8 +30,8 @@ impl<'a> Default for EvmBuilder<'a, SetGenericStage, EthChainSpec, (), EmptyDB>
fn default() -> Self {
Self {
context: Context::default(),
handler: EvmBuilder::<'_, SetGenericStage, _, _, _>::handler(
<EthChainSpec as ChainSpec>::Hardfork::default(),
handler: EthChainSpec::handler::<'a, (), EmptyDB>(
<EthChainSpec as primitives::ChainSpec>::Hardfork::default(),
),
phantom: PhantomData,
}
Expand All @@ -59,9 +56,7 @@ where

EvmBuilder {
context: Context::new(EvmContext::new(evm.inner.db), external),
handler: EvmBuilder::<'_, SetGenericStage, NewChainSpecT, _, _>::handler(
NewChainSpecT::Hardfork::default(),
),
handler: NewChainSpecT::handler::<'a, EXT, DB>(NewChainSpecT::Hardfork::default()),
phantom: PhantomData,
}
}
Expand All @@ -79,7 +74,7 @@ where
self.context.evm.with_db(EmptyDB::default()),
self.context.external,
),
handler: EvmBuilder::<'_, SetGenericStage, _, _, _>::handler(self.handler.spec_id()),
handler: ChainSpecT::handler::<'a, EXT, EmptyDB>(self.handler.spec_id()),
phantom: PhantomData,
}
}
Expand All @@ -90,7 +85,7 @@ where
) -> EvmBuilder<'a, SetGenericStage, ChainSpecT, EXT, ODB> {
EvmBuilder {
context: Context::new(self.context.evm.with_db(db), self.context.external),
handler: EvmBuilder::<'_, SetGenericStage, _, _, _>::handler(self.handler.spec_id()),
handler: ChainSpecT::handler::<'a, EXT, ODB>(self.handler.spec_id()),
phantom: PhantomData,
}
}
Expand All @@ -104,7 +99,7 @@ where
self.context.evm.with_db(WrapDatabaseRef(db)),
self.context.external,
),
handler: EvmBuilder::<'_, SetGenericStage, _, _, _>::handler(self.handler.spec_id()),
handler: ChainSpecT::handler::<'a, EXT, WrapDatabaseRef<ODB>>(self.handler.spec_id()),
phantom: PhantomData,
}
}
Expand All @@ -116,7 +111,7 @@ where
) -> EvmBuilder<'a, SetGenericStage, ChainSpecT, OEXT, DB> {
EvmBuilder {
context: Context::new(self.context.evm, external),
handler: EvmBuilder::<'_, SetGenericStage, _, _, _>::handler(self.handler.spec_id()),
handler: ChainSpecT::handler::<'a, OEXT, DB>(self.handler.spec_id()),
phantom: PhantomData,
}
}
Expand All @@ -130,7 +125,7 @@ where
self.context.evm.env = env;
EvmBuilder {
context: self.context,
handler: EvmBuilder::<'_, HandlerStage, _, _, _>::handler(spec_id),
handler: ChainSpecT::handler::<'a, EXT, DB>(spec_id),
phantom: PhantomData,
}
}
Expand All @@ -142,9 +137,7 @@ where
) -> EvmBuilder<'a, HandlerStage, ChainSpecT, OEXT, ODB> {
EvmBuilder {
context: context_with_handler_cfg.context,
handler: EvmBuilder::<'_, HandlerStage, _, _, _>::handler(
context_with_handler_cfg.spec_id,
),
handler: ChainSpecT::handler::<'a, OEXT, ODB>(context_with_handler_cfg.spec_id),
phantom: PhantomData,
}
}
Expand All @@ -158,7 +151,7 @@ where

EvmBuilder {
context: self.context,
handler: EvmBuilder::<'_, HandlerStage, _, _, _>::handler(cfg_env_and_spec_id.spec_id),
handler: ChainSpecT::handler::<'a, EXT, DB>(cfg_env_and_spec_id.spec_id),
phantom: PhantomData,
}
}
Expand Down Expand Up @@ -194,7 +187,7 @@ where
self.context.evm.with_db(EmptyDB::default()),
self.context.external,
),
handler: EvmBuilder::<'_, HandlerStage, _, _, _>::handler(self.handler.spec_id()),
handler: ChainSpecT::handler::<'a, EXT, EmptyDB>(self.handler.spec_id()),
phantom: PhantomData,
}
}
Expand All @@ -207,7 +200,7 @@ where
) -> EvmBuilder<'a, SetGenericStage, ChainSpecT, EXT, ODB> {
EvmBuilder {
context: Context::new(self.context.evm.with_db(db), self.context.external),
handler: EvmBuilder::<'_, SetGenericStage, _, _, _>::handler(self.handler.spec_id()),
handler: ChainSpecT::handler::<'a, EXT, ODB>(self.handler.spec_id()),
phantom: PhantomData,
}
}
Expand All @@ -223,7 +216,7 @@ where
self.context.evm.with_db(WrapDatabaseRef(db)),
self.context.external,
),
handler: EvmBuilder::<'_, SetGenericStage, _, _, _>::handler(self.handler.spec_id()),
handler: ChainSpecT::handler::<'a, EXT, WrapDatabaseRef<ODB>>(self.handler.spec_id()),
phantom: PhantomData,
}
}
Expand All @@ -236,7 +229,7 @@ where
) -> EvmBuilder<'a, SetGenericStage, ChainSpecT, OEXT, DB> {
EvmBuilder {
context: Context::new(self.context.evm, external),
handler: EvmBuilder::<'_, SetGenericStage, _, _, _>::handler(self.handler.spec_id()),
handler: ChainSpecT::handler::<'a, OEXT, DB>(self.handler.spec_id()),
phantom: PhantomData,
}
}
Expand Down Expand Up @@ -410,13 +403,6 @@ where
ChainSpecT:
ChainSpec<Transaction: TransactionValidation<ValidationError: From<InvalidTransaction>>>,
{
/// Creates the default handler.
///
/// This is useful for adding optimism handle register.
fn handler(spec_id: ChainSpecT::Hardfork) -> EvmHandler<'a, ChainSpecT, EXT, DB> {
Handler::mainnet_with_spec(spec_id)
}

/// Sets specification Id , that will mark the version of EVM.
/// It represent the hard fork of ethereum.
///
Expand All @@ -436,7 +422,7 @@ where

/// Resets [`Handler`] to default mainnet.
pub fn reset_handler(mut self) -> Self {
self.handler = Self::handler(self.handler.spec_id());
self.handler = ChainSpecT::handler::<'a, EXT, DB>(self.handler.spec_id());
self
}
}
Expand All @@ -456,9 +442,6 @@ mod test {
use revm_precompile::PrecompileOutput;
use std::{cell::RefCell, rc::Rc, sync::Arc};

#[cfg(feature = "optimism")]
type TestChainSpec = crate::optimism::OptimismChainSpec;
#[cfg(not(feature = "optimism"))]
type TestChainSpec = crate::primitives::EthChainSpec;

/// Custom evm context
Expand All @@ -485,9 +468,6 @@ mod test {
db.insert_account_info(to_addr, AccountInfo::new(U256::ZERO, 0, code_hash, code))
})
.modify_tx_env(|tx| {
#[cfg(feature = "optimism")]
let transact_to = &mut tx.base.transact_to;
#[cfg(not(feature = "optimism"))]
let transact_to = &mut tx.transact_to;

*transact_to = TxKind::Call(to_addr)
Expand Down Expand Up @@ -543,9 +523,6 @@ mod test {
db.insert_account_info(to_addr, AccountInfo::new(U256::ZERO, 0, code_hash, code))
})
.modify_tx_env(|tx| {
#[cfg(feature = "optimism")]
let transact_to = &mut tx.base.transact_to;
#[cfg(not(feature = "optimism"))]
let transact_to = &mut tx.transact_to;

*transact_to = TxKind::Call(to_addr)
Expand Down Expand Up @@ -594,49 +571,21 @@ mod test {
Evm::builder()
.with_chain_spec::<TestChainSpec>()
.with_empty_db()
.modify_tx_env(|tx| {
#[cfg(feature = "optimism")]
let gas_limit = &mut tx.base.gas_limit;
#[cfg(not(feature = "optimism"))]
let gas_limit = &mut tx.gas_limit;

*gas_limit = 10
})
.modify_tx_env(|tx| tx.gas_limit = 10)
.build();
Evm::builder()
.with_chain_spec::<TestChainSpec>()
.modify_tx_env(|tx| {
#[cfg(feature = "optimism")]
let gas_limit = &mut tx.base.gas_limit;
#[cfg(not(feature = "optimism"))]
let gas_limit = &mut tx.gas_limit;

*gas_limit = 10
})
.modify_tx_env(|tx| tx.gas_limit = 10)
.build();
Evm::builder()
.with_chain_spec::<TestChainSpec>()
.with_empty_db()
.modify_tx_env(|tx| {
#[cfg(feature = "optimism")]
let gas_limit = &mut tx.base.gas_limit;
#[cfg(not(feature = "optimism"))]
let gas_limit = &mut tx.gas_limit;

*gas_limit = 10
})
.modify_tx_env(|tx| tx.gas_limit = 10)
.build();
Evm::builder()
.with_chain_spec::<TestChainSpec>()
.with_empty_db()
.modify_tx_env(|tx| {
#[cfg(feature = "optimism")]
let gas_limit = &mut tx.base.gas_limit;
#[cfg(not(feature = "optimism"))]
let gas_limit = &mut tx.gas_limit;

*gas_limit = 10
})
.modify_tx_env(|tx| tx.gas_limit = 10)
.build();

// with inspector handle
Expand Down Expand Up @@ -691,9 +640,6 @@ mod test {
}
}

#[cfg(feature = "optimism")]
let spec_id = crate::optimism::OptimismSpecId::HOMESTEAD;
#[cfg(not(feature = "optimism"))]
let spec_id = crate::primitives::SpecId::HOMESTEAD;

let mut evm = Evm::builder()
Expand Down
38 changes: 38 additions & 0 deletions crates/revm/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::{
handler::{ExecutionHandler, PostExecutionHandler, PreExecutionHandler, ValidationHandler},
interpreter::opcode::InstructionTables,
primitives::{db::Database, spec_to_generic, EthChainSpec},
EvmHandler,
};

pub trait ChainSpec: crate::primitives::ChainSpec {
/// The type that contains all context information for the chain's EVM execution.
type Context: Default;

/// Creates a new handler with the given hardfork.
fn handler<'evm, EXT, DB>(hardfork: Self::Hardfork) -> EvmHandler<'evm, Self, EXT, DB>
where
DB: Database;
}

impl ChainSpec for EthChainSpec {
type Context = ();

fn handler<'evm, EXT, DB>(hardfork: Self::Hardfork) -> EvmHandler<'evm, Self, EXT, DB>
where
DB: Database,
{
spec_to_generic!(
hardfork,
EvmHandler {
spec_id: hardfork,
instruction_table: InstructionTables::new_plain::<SPEC>(),
registers: Vec::new(),
validation: ValidationHandler::new::<SPEC>(),
pre_execution: PreExecutionHandler::new::<SPEC>(),
post_execution: PostExecutionHandler::mainnet::<SPEC>(),
execution: ExecutionHandler::new::<SPEC>(),
}
)
}
}
36 changes: 5 additions & 31 deletions crates/revm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use context_precompiles::{
ContextPrecompile, ContextPrecompiles, ContextStatefulPrecompile, ContextStatefulPrecompileArc,
ContextStatefulPrecompileBox, ContextStatefulPrecompileMut,
};
use derive_where::derive_where;
pub use evm_context::EvmContext;
pub use inner_evm_context::InnerEvmContext;
use revm_interpreter::as_usize_saturated;
Expand All @@ -14,35 +15,21 @@ use crate::{
db::{Database, EmptyDB},
interpreter::{Host, LoadAccountResult, SStoreResult, SelfDestructResult},
primitives::{
Address, Block as _, Bytes, ChainSpec, Env, EthChainSpec, Log, B256, BLOCK_HASH_HISTORY,
U256,
Address, Block as _, Bytes, Env, EthChainSpec, Log, B256, BLOCK_HASH_HISTORY, U256,
},
ChainSpec,
};
use std::boxed::Box;

/// Main Context structure that contains both EvmContext and External context.
#[derive_where(Clone; ChainSpecT::Block, ChainSpecT::Context, ChainSpecT::Transaction, DB, DB::Error, EXT)]
pub struct Context<ChainSpecT: ChainSpec, EXT, DB: Database> {
/// Evm Context (internal context).
pub evm: EvmContext<ChainSpecT, DB>,
/// External contexts.
pub external: EXT,
}

impl<ChainSpecT, EXT, DB> Clone for Context<ChainSpecT, EXT, DB>
where
DB::Error: Clone,
ChainSpecT: ChainSpec<Block: Clone, Transaction: Clone>,
EXT: Clone,
DB: Database<Error: Clone> + Clone,
{
fn clone(&self) -> Self {
Self {
evm: self.evm.clone(),
external: self.external.clone(),
}
}
}

impl Default for Context<EthChainSpec, (), EmptyDB> {
fn default() -> Self {
Self::new_empty()
Expand Down Expand Up @@ -84,6 +71,7 @@ impl<ChainSpecT: ChainSpec, EXT, DB: Database> Context<ChainSpecT, EXT, DB> {
}

/// Context with handler configuration.
#[derive_where(Clone; ChainSpecT::Block, ChainSpecT::Context, ChainSpecT::Transaction, DB, DB::Error, EXT)]
pub struct ContextWithChainSpec<ChainSpecT: ChainSpec, EXT, DB: Database> {
/// Context of execution.
pub context: Context<ChainSpecT, EXT, DB>,
Expand All @@ -98,20 +86,6 @@ impl<ChainSpecT: ChainSpec, EXT, DB: Database> ContextWithChainSpec<ChainSpecT,
}
}

impl<ChainSpecT, EXT, DB> Clone for ContextWithChainSpec<ChainSpecT, EXT, DB>
where
ChainSpecT: ChainSpec<Block: Clone, Transaction: Clone>,
EXT: Clone,
DB: Database<Error: Clone> + Clone,
{
fn clone(&self) -> Self {
Self {
context: self.context.clone(),
spec_id: self.spec_id,
}
}
}

impl<ChainSpecT: ChainSpec, EXT, DB: Database> Host for Context<ChainSpecT, EXT, DB> {
type ChainSpecT = ChainSpecT;

Expand Down
Loading