From b7da8083daa88ca8041956dd7d48b1e4ddc74d9d Mon Sep 17 00:00:00 2001 From: rakita Date: Thu, 23 Jan 2025 19:20:34 +0100 Subject: [PATCH] init precompiles --- bins/revme/src/cmd/bench/transfer.rs | 12 ++- crates/context/src/instructions.rs | 81 +++++++++++++++++++++ crates/handler/src/frame.rs | 9 ++- crates/interpreter/src/instructions/host.rs | 1 + 4 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 crates/context/src/instructions.rs diff --git a/bins/revme/src/cmd/bench/transfer.rs b/bins/revme/src/cmd/bench/transfer.rs index 7d24d9a84b..d7a60da10a 100644 --- a/bins/revme/src/cmd/bench/transfer.rs +++ b/bins/revme/src/cmd/bench/transfer.rs @@ -1,8 +1,10 @@ +use std::time::Instant; + use database::{BenchmarkDB, BENCH_CALLER, BENCH_TARGET}; use revm::{ bytecode::Bytecode, primitives::{TxKind, U256}, - transact_main, Context, + Context, ExecuteEvm, }; pub fn run() { @@ -14,5 +16,11 @@ pub fn run() { tx.kind = TxKind::Call(BENCH_TARGET); tx.value = U256::from(10); }); - let _ = transact_main(&mut context); + let time = Instant::now(); + let _ = context.exec_previous(); + println!("First init: {:?}", time.elapsed()); + + let time = Instant::now(); + let _ = context.exec_previous(); + println!("Run: {:?}", time.elapsed()); } diff --git a/crates/context/src/instructions.rs b/crates/context/src/instructions.rs new file mode 100644 index 0000000000..192dc42be1 --- /dev/null +++ b/crates/context/src/instructions.rs @@ -0,0 +1,81 @@ +use interpreter::{ + table::{make_instruction_table, InstructionTable}, + Host, Interpreter, InterpreterAction, InterpreterTypes, +}; +use std::boxed::Box; + +pub trait InstructionExecutor: Clone + Default { + type InterpreterTypes: InterpreterTypes; + type CTX; + type Output; + + fn run( + &mut self, + context: &mut Self::CTX, + interpreter: &mut Interpreter, + ) -> Self::Output; +} + +#[derive(Debug)] +pub struct EthInstructionExecutor { + instruction_table: Box>, +} + +pub trait InstructionExecutorGetter { + type InstructionExecutor: InstructionExecutor; + + fn executor(&mut self) -> &mut Self::InstructionExecutor; +} + +impl Clone for EthInstructionExecutor +where + WIRE: InterpreterTypes, +{ + fn clone(&self) -> Self { + Self { + instruction_table: self.instruction_table.clone(), + } + } +} + +impl EthInstructionExecutor +where + WIRE: InterpreterTypes, + HOST: Host, +{ + pub fn new() -> Self { + Self { + instruction_table: Box::new(make_instruction_table::()), + } + } +} + +impl InstructionExecutor for EthInstructionExecutor +where + IT: InterpreterTypes, + CTX: Host, +{ + type InterpreterTypes = IT; + type CTX = CTX; + /// TODO Interpreter action could be tied to InterpreterTypes so we can + /// set custom actions from instructions. + type Output = InterpreterAction; + + fn run( + &mut self, + context: &mut Self::CTX, + interpreter: &mut Interpreter, + ) -> Self::Output { + interpreter.run_plain(&self.instruction_table, context) + } +} + +impl Default for EthInstructionExecutor +where + WIRE: InterpreterTypes, + HOST: Host, +{ + fn default() -> Self { + Self::new() + } +} diff --git a/crates/handler/src/frame.rs b/crates/handler/src/frame.rs index a58081bf52..e9a206359f 100644 --- a/crates/handler/src/frame.rs +++ b/crates/handler/src/frame.rs @@ -484,6 +484,7 @@ where Context = CTX, Error = ERROR, Output = InterpreterResult, + Spec = <::Cfg as Cfg>::Spec, >, > + InstructionExecutorGetter< InstructionExecutor: InstructionExecutor< @@ -506,10 +507,10 @@ where ) -> Result, Self::Error> { let memory = Rc::new(RefCell::new(SharedMemory::new())); - // Load precompiles addresses as warm. - for address in frame_context.precompiles().warm_addresses() { - context.journal().warm_account(address); - } + frame_context.precompiles().set_spec(context.cfg().spec()); + context + .journal() + .warm_precompiles(frame_context.precompiles().warm_addresses().collect()); memory.borrow_mut().new_context(); Self::init_with_context(0, frame_input, memory, context, frame_context) diff --git a/crates/interpreter/src/instructions/host.rs b/crates/interpreter/src/instructions/host.rs index c3f94a33c6..0da9e40aa7 100644 --- a/crates/interpreter/src/instructions/host.rs +++ b/crates/interpreter/src/instructions/host.rs @@ -294,6 +294,7 @@ pub fn selfdestruct( if !interpreter.runtime_flag.spec_id().is_enabled_in(LONDON) && !res.previously_destroyed { interpreter.control.gas().record_refund(gas::SELFDESTRUCT) } + gas!( interpreter, gas::selfdestruct_cost(interpreter.runtime_flag.spec_id(), res)