Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 1 addition & 10 deletions crates/handler/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ where
HOST: Host,
{
/// Returns `EthInstructions` with mainnet spec.
#[deprecated(since = "0.2.0", note = "use new_mainnet_with_spec instead")]
pub fn new_mainnet() -> Self {
let spec = SpecId::default();
Self::new(instruction_table_gas_changes_spec(spec), spec)
Expand Down Expand Up @@ -83,13 +84,3 @@ where
&self.instruction_table
}
}

impl<WIRE, HOST> Default for EthInstructions<WIRE, HOST>
where
WIRE: InterpreterTypes,
HOST: Host,
{
fn default() -> Self {
Self::new_mainnet()
}
}
10 changes: 0 additions & 10 deletions crates/handler/src/precompile_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ impl Clone for EthPrecompiles {
}
}

impl Default for EthPrecompiles {
fn default() -> Self {
let spec = SpecId::default();
Self {
precompiles: Precompiles::new(PrecompileSpecId::from_spec_id(spec)),
spec,
}
}
}

impl<CTX: ContextTr> PrecompileProvider<CTX> for EthPrecompiles {
type Output = InterpreterResult;

Expand Down
141 changes: 24 additions & 117 deletions examples/cheatcode_inspector/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ use revm::{
Block, JournalTr, Transaction,
},
database::InMemoryDB,
handler::{
instructions::{EthInstructions, InstructionProvider},
EthPrecompiles, PrecompileProvider,
},
handler::{instructions::EthInstructions, EthPrecompiles},
inspector::{inspectors::TracerEip3155, JournalExt},
interpreter::{
interpreter::EthInterpreter, CallInputs, CallOutcome, InterpreterResult, SStoreResult,
SelfDestructResult, StateLoad,
interpreter::EthInterpreter, CallInputs, CallOutcome, SStoreResult, SelfDestructResult,
StateLoad,
},
primitives::{
hardfork::SpecId, Address, AddressMap, AddressSet, HashSet, Log, StorageKey, StorageValue,
Expand Down Expand Up @@ -349,14 +346,7 @@ impl JournalExt for Backend {
trait DatabaseExt: JournalTr {
/// Mimics `DatabaseExt::transact`
/// See `commit_transaction` for the generics
fn method_that_takes_inspector_as_argument<
InspectorT,
BlockT,
TxT,
CfgT,
InstructionProviderT,
PrecompileT,
>(
fn method_that_takes_inspector_as_argument<InspectorT, BlockT, TxT, CfgT>(
&mut self,
env: Env<BlockT, TxT, CfgT>,
inspector: InspectorT,
Expand All @@ -365,44 +355,21 @@ trait DatabaseExt: JournalTr {
InspectorT: Inspector<Context<BlockT, TxT, CfgT, InMemoryDB, Backend>, EthInterpreter>,
BlockT: Block,
TxT: Transaction + Clone,
CfgT: Cfg,
InstructionProviderT: InstructionProvider<
Context = Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
InterpreterTypes = EthInterpreter,
> + Default,
PrecompileT: PrecompileProvider<
Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
Output = InterpreterResult,
> + Default;
CfgT: Cfg;

/// Mimics `DatabaseExt::roll_fork_to_transaction`
fn method_that_constructs_inspector<BlockT, TxT, CfgT, InstructionProviderT, PrecompileT>(
fn method_that_constructs_inspector<BlockT, TxT, CfgT>(
&mut self,
env: Env<BlockT, TxT, CfgT>,
) -> anyhow::Result<()>
where
BlockT: Block,
TxT: Transaction + Clone,
CfgT: Cfg,
InstructionProviderT: InstructionProvider<
Context = Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
InterpreterTypes = EthInterpreter,
> + Default,
PrecompileT: PrecompileProvider<
Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
Output = InterpreterResult,
> + Default;
CfgT: Cfg;
}

impl DatabaseExt for Backend {
fn method_that_takes_inspector_as_argument<
InspectorT,
BlockT,
TxT,
CfgT,
InstructionProviderT,
PrecompileT,
>(
fn method_that_takes_inspector_as_argument<InspectorT, BlockT, TxT, CfgT>(
&mut self,
env: Env<BlockT, TxT, CfgT>,
inspector: InspectorT,
Expand All @@ -412,49 +379,23 @@ impl DatabaseExt for Backend {
BlockT: Block,
TxT: Transaction + Clone,
CfgT: Cfg,
InstructionProviderT: InstructionProvider<
Context = Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
InterpreterTypes = EthInterpreter,
> + Default,
PrecompileT: PrecompileProvider<
Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
Output = InterpreterResult,
> + Default,
{
commit_transaction::<InspectorT, BlockT, TxT, CfgT, InstructionProviderT, PrecompileT>(
self, env, inspector,
)?;
commit_transaction(self, env, inspector)?;
self.method_with_inspector_counter += 1;
Ok(())
}

fn method_that_constructs_inspector<BlockT, TxT, CfgT, InstructionProviderT, PrecompileT>(
fn method_that_constructs_inspector<BlockT, TxT, CfgT>(
&mut self,
env: Env<BlockT, TxT, CfgT>,
) -> anyhow::Result<()>
where
BlockT: Block,
TxT: Transaction + Clone,
CfgT: Cfg,
InstructionProviderT: InstructionProvider<
Context = Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
InterpreterTypes = EthInterpreter,
> + Default,
PrecompileT: PrecompileProvider<
Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
Output = InterpreterResult,
> + Default,
{
let inspector = TracerEip3155::new(Box::new(std::io::sink()));
commit_transaction::<
// Generic interpreter types are not supported yet in the `Evm` implementation
TracerEip3155,
BlockT,
TxT,
CfgT,
InstructionProviderT,
PrecompileT,
>(self, env, inspector)?;
commit_transaction(self, env, inspector)?;

self.method_without_inspector_counter += 1;
Ok(())
Expand All @@ -464,25 +405,16 @@ impl DatabaseExt for Backend {
/// An REVM inspector that intercepts calls to the cheatcode address and executes them with the help of the
/// `DatabaseExt` trait.
#[derive(Clone, Default)]
struct Cheatcodes<BlockT, TxT, CfgT, InstructionProviderT, PrecompileT> {
struct Cheatcodes<BlockT, TxT, CfgT> {
call_count: usize,
phantom: core::marker::PhantomData<(BlockT, TxT, CfgT, InstructionProviderT, PrecompileT)>,
phantom: core::marker::PhantomData<(BlockT, TxT, CfgT)>,
}

impl<BlockT, TxT, CfgT, InstructionProviderT, PrecompileT>
Cheatcodes<BlockT, TxT, CfgT, InstructionProviderT, PrecompileT>
impl<BlockT, TxT, CfgT> Cheatcodes<BlockT, TxT, CfgT>
where
BlockT: Block + Clone,
TxT: Transaction + Clone,
CfgT: Cfg + Clone,
InstructionProviderT: InstructionProvider<
Context = Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
InterpreterTypes = EthInterpreter,
> + Default,
PrecompileT: PrecompileProvider<
Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
Output = InterpreterResult,
> + Default,
{
fn apply_cheatcode(
&mut self,
Expand All @@ -496,7 +428,7 @@ where
// `transact` cheatcode would do this
context
.journal_mut()
.method_that_takes_inspector_as_argument::<_, _, _, _, InstructionProviderT, PrecompileT>(
.method_that_takes_inspector_as_argument(
Env {
block: block.clone(),
tx: tx.clone(),
Expand All @@ -508,28 +440,17 @@ where
// `rollFork(bytes32 transaction)` cheatcode would do this
context
.journal_mut()
.method_that_constructs_inspector::<_, _, _, InstructionProviderT, PrecompileT>(
Env { block, tx, cfg },
)?;
.method_that_constructs_inspector(Env { block, tx, cfg })?;
Ok(())
}
}

impl<BlockT, TxT, CfgT, InstructionProviderT, PrecompileT>
Inspector<Context<BlockT, TxT, CfgT, InMemoryDB, Backend>>
for Cheatcodes<BlockT, TxT, CfgT, InstructionProviderT, PrecompileT>
impl<BlockT, TxT, CfgT> Inspector<Context<BlockT, TxT, CfgT, InMemoryDB, Backend>>
for Cheatcodes<BlockT, TxT, CfgT>
where
BlockT: Block + Clone,
TxT: Transaction + Clone,
CfgT: Cfg + Clone,
InstructionProviderT: InstructionProvider<
Context = Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
InterpreterTypes = EthInterpreter,
> + Default,
PrecompileT: PrecompileProvider<
Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
Output = InterpreterResult,
> + Default,
{
/// Note that precompiles are no longer accessible via `EvmContext::precompiles`.
fn call(
Expand Down Expand Up @@ -572,7 +493,7 @@ impl Env<BlockEnv, TxEnv, CfgEnv> {

/// Executes a transaction and runs the inspector using the `Backend` as the state.
/// Mimics `commit_transaction` <https://github.com/foundry-rs/foundry/blob/25cc1ac68b5f6977f23d713c01ec455ad7f03d21/crates/evm/core/src/backend/mod.rs#L1931>
fn commit_transaction<InspectorT, BlockT, TxT, CfgT, InstructionProviderT, PrecompileT>(
fn commit_transaction<InspectorT, BlockT, TxT, CfgT>(
backend: &mut Backend,
env: Env<BlockT, TxT, CfgT>,
inspector: InspectorT,
Expand All @@ -582,14 +503,6 @@ where
BlockT: Block,
TxT: Transaction + Clone,
CfgT: Cfg,
InstructionProviderT: InstructionProvider<
Context = Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
InterpreterTypes = EthInterpreter,
> + Default,
PrecompileT: PrecompileProvider<
Context<BlockT, TxT, CfgT, InMemoryDB, Backend>,
Output = InterpreterResult,
> + Default,
{
// Create new journaled state and backend with the same DB and journaled state as the original for the transaction.
// This new backend and state will be discarded after the transaction is done and the changes are applied to the
Expand All @@ -611,8 +524,8 @@ where
let mut evm = Evm::new_with_inspector(
context,
inspector,
InstructionProviderT::default(),
PrecompileT::default(),
EthInstructions::new_mainnet_with_spec(SpecId::default()),
EthPrecompiles::new(SpecId::default()),
);

let state = evm.inspect_tx(tx)?.state;
Expand Down Expand Up @@ -642,13 +555,7 @@ fn update_state<DB: Database>(state: &mut EvmState, db: &mut DB) -> Result<(), D

fn main() -> anyhow::Result<()> {
let backend = Backend::new(SpecId::default(), InMemoryDB::default());
let mut inspector = Cheatcodes::<
BlockEnv,
TxEnv,
CfgEnv,
EthInstructions<EthInterpreter, Context<BlockEnv, TxEnv, CfgEnv, InMemoryDB, Backend>>,
EthPrecompiles,
>::default();
let mut inspector = Cheatcodes::<BlockEnv, TxEnv, CfgEnv>::default();
let env = Env::mainnet();
let tx = env.tx.clone();

Expand All @@ -665,8 +572,8 @@ fn main() -> anyhow::Result<()> {
let mut evm = Evm::new_with_inspector(
context,
&mut inspector,
EthInstructions::default(),
EthPrecompiles::default(),
EthInstructions::new_mainnet_with_spec(SpecId::default()),
EthPrecompiles::new(SpecId::default()),
);
evm.inspect_tx(tx)?;

Expand Down
5 changes: 3 additions & 2 deletions examples/custom_opcodes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use revm::{
interpreter_types::{Immediates, Jumps},
Instruction, InstructionContext,
},
primitives::hardfork::SpecId,
primitives::TxKind,
state::Bytecode,
Context, InspectEvm, MainContext,
Expand All @@ -37,7 +38,7 @@ pub fn main() {
)));

// Create a new instruction set with our mainnet opcodes.
let mut instructions = EthInstructions::new_mainnet();
let mut instructions = EthInstructions::new_mainnet_with_spec(SpecId::default());
// insert our custom opcode
instructions.insert_instruction(
MY_STATIC_JUMP,
Expand All @@ -51,7 +52,7 @@ pub fn main() {
);

// Create a new EVM instance.
let mut evm = Evm::new(ctx, instructions, EthPrecompiles::default())
let mut evm = Evm::new(ctx, instructions, EthPrecompiles::new(SpecId::default()))
.with_inspector(TracerEip3155::new_stdout().without_summary());

// inspect the transaction.
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_precompile_journal/src/custom_evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
Self(Evm {
ctx,
inspector,
instruction: EthInstructions::new_mainnet(),
instruction: EthInstructions::new_mainnet_with_spec(SpecId::CANCUN),
precompiles: CustomPrecompileProvider::new_with_spec(SpecId::CANCUN),
frame_stack: FrameStack::new(),
})
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_precompile_journal/src/precompile_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct CustomPrecompileProvider {
impl CustomPrecompileProvider {
pub fn new_with_spec(spec: SpecId) -> Self {
Self {
inner: EthPrecompiles::default(),
inner: EthPrecompiles::new(spec),
spec,
}
}
Expand All @@ -44,7 +44,7 @@ where
}
self.spec = spec;
// Create a new inner provider with the new spec
self.inner = EthPrecompiles::default();
self.inner = EthPrecompiles::new(spec);
true
}

Expand Down
5 changes: 3 additions & 2 deletions examples/my_evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use revm::{
},
inspector::{InspectorEvmTr, JournalExt},
interpreter::interpreter::EthInterpreter,
primitives::hardfork::SpecId,
Database, Inspector,
};

Expand Down Expand Up @@ -47,8 +48,8 @@ impl<CTX: ContextTr, INSP> MyEvm<CTX, INSP> {
Self(Evm {
ctx,
inspector,
instruction: EthInstructions::new_mainnet(),
precompiles: EthPrecompiles::default(),
instruction: EthInstructions::new_mainnet_with_spec(SpecId::default()),
precompiles: EthPrecompiles::new(SpecId::default()),
frame_stack: FrameStack::new(),
})
}
Expand Down
Loading