Skip to content

Commit

Permalink
init precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Jan 23, 2025
1 parent 12acb11 commit b7da808
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 6 deletions.
12 changes: 10 additions & 2 deletions bins/revme/src/cmd/bench/transfer.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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());
}
81 changes: 81 additions & 0 deletions crates/context/src/instructions.rs
Original file line number Diff line number Diff line change
@@ -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::InterpreterTypes>,
) -> Self::Output;
}

#[derive(Debug)]
pub struct EthInstructionExecutor<WIRE: InterpreterTypes, HOST> {
instruction_table: Box<InstructionTable<WIRE, HOST>>,
}

pub trait InstructionExecutorGetter {
type InstructionExecutor: InstructionExecutor;

fn executor(&mut self) -> &mut Self::InstructionExecutor;
}

impl<WIRE, HOST> Clone for EthInstructionExecutor<WIRE, HOST>
where
WIRE: InterpreterTypes,
{
fn clone(&self) -> Self {
Self {
instruction_table: self.instruction_table.clone(),
}
}
}

impl<WIRE, HOST> EthInstructionExecutor<WIRE, HOST>
where
WIRE: InterpreterTypes,
HOST: Host,
{
pub fn new() -> Self {
Self {
instruction_table: Box::new(make_instruction_table::<WIRE, HOST>()),
}
}
}

impl<IT, CTX> InstructionExecutor for EthInstructionExecutor<IT, CTX>
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::InterpreterTypes>,
) -> Self::Output {
interpreter.run_plain(&self.instruction_table, context)
}
}

impl<WIRE, HOST> Default for EthInstructionExecutor<WIRE, HOST>
where
WIRE: InterpreterTypes,
HOST: Host,
{
fn default() -> Self {
Self::new()
}
}
9 changes: 5 additions & 4 deletions crates/handler/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ where
Context = CTX,
Error = ERROR,
Output = InterpreterResult,
Spec = <<CTX as CfgGetter>::Cfg as Cfg>::Spec,
>,
> + InstructionExecutorGetter<
InstructionExecutor: InstructionExecutor<
Expand All @@ -506,10 +507,10 @@ where
) -> Result<ItemOrResult<Self, Self::FrameResult>, 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)
Expand Down
1 change: 1 addition & 0 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ pub fn selfdestruct<WIRE: InterpreterTypes, H: Host + ?Sized>(
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)
Expand Down

0 comments on commit b7da808

Please sign in to comment.