Skip to content

Commit

Permalink
chore: docs and cleanup (rm Custom Inst) (#2151)
Browse files Browse the repository at this point in the history
* code cleanup

* chore: docs and cleanup (rm Custom Inst)

* clippy

* typos
  • Loading branch information
rakita authored Mar 6, 2025
1 parent 435e630 commit 39a3a2b
Show file tree
Hide file tree
Showing 21 changed files with 277 additions and 287 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.

17 changes: 9 additions & 8 deletions crates/handler/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ where
Instructions: InstructionProvider<
Context = EVM::Context,
InterpreterTypes = EthInterpreter,
Output = InterpreterAction,
>,
>,
ERROR: From<ContextTrDbError<EVM::Context>> + From<PrecompileError>,
Expand Down Expand Up @@ -519,7 +518,6 @@ where
Instructions: InstructionProvider<
Context = EVM::Context,
InterpreterTypes = EthInterpreter,
Output = InterpreterAction,
>,
>,
ERROR: From<ContextTrDbError<EVM::Context>> + From<PrecompileError>,
Expand Down Expand Up @@ -629,7 +627,7 @@ where
let interpreter = &mut self.interpreter;
let mem_length = outcome.memory_length();
let mem_start = outcome.memory_start();
*interpreter.return_data.buffer_mut() = outcome.result.output;
interpreter.return_data.set_buffer(outcome.result.output);

let target_len = min(mem_length, returned_len);

Expand Down Expand Up @@ -675,13 +673,14 @@ where
let instruction_result = *outcome.instruction_result();
let interpreter = &mut self.interpreter;

let buffer = interpreter.return_data.buffer_mut();
if instruction_result == InstructionResult::Revert {
// Save data to return data buffer if the create reverted
*buffer = outcome.output().to_owned()
interpreter
.return_data
.set_buffer(outcome.output().to_owned());
} else {
// Otherwise clear it. Note that RETURN opcode should abort.
buffer.clear();
interpreter.return_data.clear();
};

assert_ne!(
Expand Down Expand Up @@ -710,10 +709,12 @@ where
let interpreter = &mut self.interpreter;
if instruction_result == InstructionResult::Revert {
// Save data to return data buffer if the create reverted
*interpreter.return_data.buffer_mut() = outcome.output().to_owned()
interpreter
.return_data
.set_buffer(outcome.output().to_owned());
} else {
// Otherwise clear it. Note that RETURN opcode should abort.
interpreter.return_data.buffer_mut().clear();
interpreter.return_data.clear()
};

assert_ne!(
Expand Down
14 changes: 10 additions & 4 deletions crates/handler/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use context_interface::{
Cfg, Database, Journal, Transaction,
};
use core::mem;
use interpreter::{FrameInput, Host, InitialAndFloorGas, Interpreter, InterpreterAction};
use interpreter::{
FrameInput, Host, InitialAndFloorGas, Interpreter, InterpreterAction, InterpreterTypes,
};
use precompile::PrecompileError;
use primitives::Log;
use state::EvmState;
Expand All @@ -37,7 +39,10 @@ impl<
impl<CTX, INSP, I, P> EvmTr for Evm<CTX, INSP, I, P>
where
CTX: ContextTr + Host,
I: InstructionProvider<Context = CTX, Output = InterpreterAction>,
I: InstructionProvider<
Context = CTX,
InterpreterTypes: InterpreterTypes<Output = InterpreterAction>,
>,
{
type Context = CTX;
type Instructions = I;
Expand All @@ -49,7 +54,8 @@ where
interpreter: &mut Interpreter<
<Self::Instructions as InstructionProvider>::InterpreterTypes,
>,
) -> <Self::Instructions as InstructionProvider>::Output {
) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output
{
let context = &mut self.data.ctx;
let instructions = &mut self.instruction;
interpreter.run_plain(instructions.instruction_table(), context)
Expand Down Expand Up @@ -86,7 +92,7 @@ pub trait EvmTr {
interpreter: &mut Interpreter<
<Self::Instructions as InstructionProvider>::InterpreterTypes,
>,
) -> <Self::Instructions as InstructionProvider>::Output;
) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output;

fn ctx(&mut self) -> &mut Self::Context;

Expand Down
37 changes: 16 additions & 21 deletions crates/handler/src/instructions.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use auto_impl::auto_impl;
use context_interface::ContextTr;
use interpreter::{
table::{make_instruction_table, InstructionTable},
Host, Interpreter, InterpreterAction, InterpreterTypes,
instructions::{instruction_table, InstructionTable},
Host, Instruction, InterpreterTypes,
};
use std::rc::Rc;
use std::boxed::Box;

/// Stores instructions for EVM.
#[auto_impl(&, Arc, Rc)]
pub trait InstructionProvider {
/// Context type.
type Context;
/// Interpreter types.
type InterpreterTypes: InterpreterTypes;
type Output;

/// Returns the instruction table that is used by EvmTr to execute instructions.
fn instruction_table(&self) -> &InstructionTable<Self::InterpreterTypes, Self::Context>;
}

/// Ethereum instruction contains list of mainnet instructions that is used for Interpreter execution.
pub struct EthInstructions<WIRE: InterpreterTypes, HOST> {
pub instruction_table: Rc<InstructionTable<WIRE, HOST>>,
pub instruction_table: Box<InstructionTable<WIRE, HOST>>,
}

impl<WIRE, HOST> Clone for EthInstructions<WIRE, HOST>
Expand All @@ -36,26 +38,22 @@ where
WIRE: InterpreterTypes,
HOST: Host,
{
/// Returns `EthInstructions` with mainnet spec.
pub fn new_mainnet() -> Self {
Self::new(make_instruction_table::<WIRE, HOST>())
Self::new(instruction_table::<WIRE, HOST>())
}

/// Rerurns new `EthInstructions` with custom instruction table.
pub fn new(base_table: InstructionTable<WIRE, HOST>) -> Self {
Self {
instruction_table: Rc::new(base_table),
instruction_table: Box::new(base_table),
}
}
}

pub trait ContextInspectRun {
type InterpreterTypes: InterpreterTypes;
type Context: ContextTr + Host;

fn run_context(
&mut self,
interpretere: Interpreter<Self::InterpreterTypes>,
instructions: &InstructionTable<Self::InterpreterTypes, Self::Context>,
);
/// Inserts a new instruction into the instruction table.s
pub fn insert_instruction(&mut self, opcode: u8, instruction: Instruction<WIRE, HOST>) {
self.instruction_table[opcode as usize] = instruction;
}
}

impl<IT, CTX> InstructionProvider for EthInstructions<IT, CTX>
Expand All @@ -65,9 +63,6 @@ where
{
type InterpreterTypes = IT;
type Context = CTX;
/// TODO Interpreter action could be tied to InterpreterTypes so we can
/// set custom actions from instructions.
type Output = InterpreterAction;

fn instruction_table(&self) -> &InstructionTable<Self::InterpreterTypes, Self::Context> {
&self.instruction_table
Expand Down
4 changes: 2 additions & 2 deletions crates/inspector/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ use handler::{
execution, EvmTr, Frame, FrameInitOrResult, FrameOrResult, FrameResult, Handler, ItemOrResult,
};
use interpreter::{
instructions::InstructionTable,
interpreter::EthInterpreter,
interpreter_types::{Jumps, LoopControl},
table::InstructionTable,
CallInputs, CallOutcome, CreateInputs, CreateOutcome, EOFCreateInputs, FrameInput, Host,
InitialAndFloorGas, InstructionResult, Interpreter, InterpreterAction, InterpreterTypes,
};
use primitives::{Address, Log, U256};
use state::EvmState;
use std::{vec, vec::Vec};

/// EVM [Interpreter] callbacks.
/// EVM hooks into execution.
#[auto_impl(&mut, Box)]
pub trait Inspector<CTX, INTR: InterpreterTypes = EthInterpreter> {
/// Called before the interpreter is initialized.
Expand Down
11 changes: 7 additions & 4 deletions crates/inspector/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ pub trait InspectorEvmTr: EvmTr {
interpreter: &mut Interpreter<
<Self::Instructions as InstructionProvider>::InterpreterTypes,
>,
) -> <Self::Instructions as InstructionProvider>::Output;
) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output;
}

impl<CTX, INSP, I, P> InspectorEvmTr for Evm<CTX, INSP, I, P>
where
CTX: ContextTr<Journal: JournalExt> + ContextSetters,
I: InstructionProvider<Context = CTX, Output = InterpreterAction>,
I: InstructionProvider<
Context = CTX,
InterpreterTypes: InterpreterTypes<Output = InterpreterAction>,
>,
INSP: Inspector<CTX, I::InterpreterTypes>,
{
type Inspector = INSP;
Expand All @@ -47,7 +50,8 @@ where
interpreter: &mut Interpreter<
<Self::Instructions as InstructionProvider>::InterpreterTypes,
>,
) -> <Self::Instructions as InstructionProvider>::Output {
) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output
{
let context = &mut self.data.ctx;
let instructions = &mut self.instruction;
let inspector = &mut self.data.inspector;
Expand Down Expand Up @@ -80,7 +84,6 @@ where
Instructions: InstructionProvider<
Context = EVM::Context,
InterpreterTypes = EthInterpreter,
Output = InterpreterAction,
>,
> + InspectorEvmTr,
ERROR: From<ContextTrDbError<EVM::Context>> + From<PrecompileError>,
Expand Down
1 change: 0 additions & 1 deletion crates/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ serde = { workspace = true, features = ["derive", "rc"], optional = true }
[dev-dependencies]
database-interface.workspace = true
walkdir.workspace = true
serde_json.workspace = true
bincode.workspace = true

[features]
Expand Down
106 changes: 106 additions & 0 deletions crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,109 @@ impl<CTX: ContextTr> Host for CTX {
.ok()
}
}

/// Dummy host that implements [`Host`] trait and returns all default values.
pub struct DummyHost;

impl Host for DummyHost {
fn basefee(&self) -> U256 {
U256::ZERO
}

fn blob_gasprice(&self) -> U256 {
U256::ZERO
}

fn gas_limit(&self) -> U256 {
U256::ZERO
}

fn difficulty(&self) -> U256 {
U256::ZERO
}

fn prevrandao(&self) -> Option<U256> {
None
}

fn block_number(&self) -> u64 {
0
}

fn timestamp(&self) -> U256 {
U256::ZERO
}

fn beneficiary(&self) -> Address {
Address::ZERO
}

fn chain_id(&self) -> U256 {
U256::ZERO
}

fn effective_gas_price(&self) -> U256 {
U256::ZERO
}

fn caller(&self) -> Address {
Address::ZERO
}

fn blob_hash(&self, _number: usize) -> Option<U256> {
None
}

fn max_initcode_size(&self) -> usize {
0
}

fn block_hash(&mut self, _number: u64) -> Option<B256> {
None
}

fn selfdestruct(
&mut self,
_address: Address,
_target: Address,
) -> Option<StateLoad<SelfDestructResult>> {
None
}

fn log(&mut self, _log: Log) {}

fn sstore(
&mut self,
_address: Address,
_key: U256,
_value: U256,
) -> Option<StateLoad<SStoreResult>> {
None
}

fn sload(&mut self, _address: Address, _key: U256) -> Option<StateLoad<U256>> {
None
}

fn tstore(&mut self, _address: Address, _key: U256, _value: U256) {}

fn tload(&mut self, _address: Address, _key: U256) -> U256 {
U256::ZERO
}

fn balance(&mut self, _address: Address) -> Option<StateLoad<U256>> {
None
}

fn load_account_delegated(&mut self, _address: Address) -> Option<StateLoad<AccountLoad>> {
None
}

fn load_account_code(&mut self, _address: Address) -> Option<StateLoad<Bytes>> {
None
}

fn load_account_code_hash(&mut self, _address: Address) -> Option<StateLoad<B256>> {
None
}
}
Loading

0 comments on commit 39a3a2b

Please sign in to comment.