Skip to content

Commit

Permalink
Move only State to associate type
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas committed Dec 6, 2023
1 parent 6ba4c6d commit 92504d6
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 185 deletions.
24 changes: 18 additions & 6 deletions interpreter/src/etable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@ use crate::{
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};

pub trait EtableSet<S, H, Tr> {
pub trait EtableSet {
type State;
type Handle;
type Trap;

fn eval(
&self,
machine: &mut Machine<S>,
handle: &mut H,
machine: &mut Machine<Self::State>,
handle: &mut Self::Handle,
opcode: Opcode,
position: usize,
) -> Control<Tr>;
) -> Control<Self::Trap>;
}

impl<S, H, Tr, F> EtableSet<S, H, Tr> for Etable<S, H, Tr, F>
impl<S, H, Tr, F> EtableSet for Etable<S, H, Tr, F>
where
F: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
{
type State = S;
type Handle = H;
type Trap = Tr;

fn eval(
&self,
machine: &mut Machine<S>,
Expand All @@ -30,11 +38,15 @@ where
}
}

impl<S, H, Tr, F1, F2> EtableSet<S, H, Tr> for (Etable<S, H, Tr, F1>, Etable<S, H, Tr, F2>)
impl<S, H, Tr, F1, F2> EtableSet for (Etable<S, H, Tr, F1>, Etable<S, H, Tr, F2>)
where
F1: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
F2: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
{
type State = S;
type Handle = H;
type Trap = Tr;

fn eval(
&self,
machine: &mut Machine<S>,
Expand Down
48 changes: 25 additions & 23 deletions interpreter/src/interpreter/etable.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
use crate::interpreter::{Interpreter, RunInterpreter, StepInterpreter};
use crate::{
Capture, Control, EtableSet, ExitError, ExitException, ExitFatal, ExitResult, ExitSucceed,
Interpreter, Machine, Opcode, Stack, StepInterpreter, Valids,
Machine, Opcode, Stack, Valids,
};
use alloc::vec::Vec;
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};

pub struct EtableInterpreter<'etable, S, H, Tr, ES> {
pub struct EtableInterpreter<'etable, S, ES> {
valids: Valids,
position: usize,
machine: Machine<S>,
etable: &'etable ES,
_marker: PhantomData<(H, Tr)>,
}

impl<'etable, S, H, Tr, ES> Deref for EtableInterpreter<'etable, S, H, Tr, ES> {
impl<'etable, S, ES> Deref for EtableInterpreter<'etable, S, ES> {
type Target = Machine<S>;

fn deref(&self) -> &Machine<S> {
&self.machine
}
}

impl<'etable, S, H, Tr, ES> DerefMut for EtableInterpreter<'etable, S, H, Tr, ES> {
impl<'etable, S, ES> DerefMut for EtableInterpreter<'etable, S, ES> {
fn deref_mut(&mut self) -> &mut Machine<S> {
&mut self.machine
}
}

impl<'etable, S, H, Tr, ES> EtableInterpreter<'etable, S, H, Tr, ES>
impl<'etable, S, ES> EtableInterpreter<'etable, S, ES>
where
ES: EtableSet<S, H, Tr>,
ES: EtableSet<State = S>,
{
/// Return a reference of the program counter.
pub const fn position(&self) -> usize {
Expand All @@ -45,7 +44,6 @@ where
valids,
position: 0,
etable,
_marker: PhantomData,
}
}

Expand Down Expand Up @@ -86,10 +84,9 @@ where
}
}

impl<'etable, S, H, Tr, ES> Interpreter<S, H, Tr> for EtableInterpreter<'etable, S, H, Tr, ES>
where
ES: EtableSet<S, H, Tr>,
{
impl<'etable, S, ES> Interpreter for EtableInterpreter<'etable, S, ES> {
type State = S;

fn machine(&self) -> &Machine<S> {
&self.machine
}
Expand All @@ -102,6 +99,19 @@ where
(self.machine.state, self.machine.retval)
}

fn advance(&mut self) {
if self.position == self.code.len() {
return;
}

self.position += 1;
}
}

impl<'etable, S, H, Tr, ES> RunInterpreter<H, Tr> for EtableInterpreter<'etable, S, ES>
where
ES: EtableSet<State = S, Handle = H, Trap = Tr>,
{
fn run(&mut self, handle: &mut H) -> Capture<ExitResult, Tr> {
loop {
match self.step(handle) {
Expand All @@ -110,19 +120,11 @@ where
}
}
}

fn advance(&mut self) {
if self.position == self.code.len() {
return;
}

self.position += 1;
}
}

impl<'etable, S, H, Tr, ES> StepInterpreter<S, H, Tr> for EtableInterpreter<'etable, S, H, Tr, ES>
impl<'etable, S, H, Tr, ES> StepInterpreter<H, Tr> for EtableInterpreter<'etable, S, ES>
where
ES: EtableSet<S, H, Tr>,
ES: EtableSet<State = S, Handle = H, Trap = Tr>,
{
#[inline]
fn step(&mut self, handle: &mut H) -> Result<(), Capture<ExitResult, Tr>> {
Expand Down
17 changes: 11 additions & 6 deletions interpreter/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ pub use self::etable::EtableInterpreter;
use crate::{Capture, ExitResult, Machine};
use alloc::vec::Vec;

pub trait Interpreter<S, H, Tr> {
fn machine(&self) -> &Machine<S>;
fn machine_mut(&mut self) -> &mut Machine<S>;
pub trait Interpreter {
type State;

fn deconstruct(self) -> (S, Vec<u8>);
fn run(&mut self, handle: &mut H) -> Capture<ExitResult, Tr>;
fn machine(&self) -> &Machine<Self::State>;
fn machine_mut(&mut self) -> &mut Machine<Self::State>;

fn deconstruct(self) -> (Self::State, Vec<u8>);
fn advance(&mut self);
}

pub trait StepInterpreter<S, H, Tr>: Interpreter<S, H, Tr> {
pub trait RunInterpreter<H, Tr>: Interpreter {
fn run(&mut self, handle: &mut H) -> Capture<ExitResult, Tr>;
}

pub trait StepInterpreter<H, Tr>: Interpreter {
fn step(&mut self, handle: &mut H) -> Result<(), Capture<ExitResult, Tr>>;
}
3 changes: 1 addition & 2 deletions interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern crate alloc;
mod error;
mod etable;
pub mod eval;
mod interpreter;
pub mod interpreter;
mod memory;
mod opcode;
mod runtime;
Expand All @@ -20,7 +20,6 @@ mod valids;

pub use crate::error::{Capture, ExitError, ExitException, ExitFatal, ExitResult, ExitSucceed};
pub use crate::etable::{Control, Efn, Etable, EtableSet};
pub use crate::interpreter::{EtableInterpreter, Interpreter, StepInterpreter};
pub use crate::memory::Memory;
pub use crate::opcode::Opcode;
pub use crate::runtime::{
Expand Down
28 changes: 12 additions & 16 deletions interpreter/src/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use crate::utils::{h256_to_u256, u256_to_usize};
use crate::{
Context, ExitError, ExitException, ExitResult, Interpreter, Machine, Memory, RuntimeBackend,
RuntimeState, Transfer,
interpreter::Interpreter, Context, ExitError, ExitException, ExitResult, Machine, Memory,
RuntimeBackend, RuntimeState, Transfer,
};
use alloc::vec::Vec;
use core::cmp::{max, min};
Expand Down Expand Up @@ -297,17 +297,15 @@ impl CallTrapData {
}
}

pub fn feedback<
S: AsRef<RuntimeState> + AsMut<RuntimeState>,
H,
Tr,
I: Interpreter<S, H, Tr>,
>(
pub fn feedback<I: Interpreter>(
self,
reason: ExitResult,
retbuf: Vec<u8>,
interpreter: &mut I,
) -> Result<(), ExitError> {
) -> Result<(), ExitError>
where
I::State: AsRef<RuntimeState> + AsMut<RuntimeState>,
{
let target_len = min(self.out_len, U256::from(retbuf.len()));
let out_offset = self.out_offset;

Expand Down Expand Up @@ -466,17 +464,15 @@ impl CreateTrapData {
})
}

pub fn feedback<
S: AsRef<RuntimeState> + AsMut<RuntimeState>,
H,
Tr,
I: Interpreter<S, H, Tr>,
>(
pub fn feedback<I: Interpreter>(
self,
reason: Result<H160, ExitError>,
retbuf: Vec<u8>,
interpreter: &mut I,
) -> Result<(), ExitError> {
) -> Result<(), ExitError>
where
I::State: AsRef<RuntimeState> + AsMut<RuntimeState>,
{
let ret = match reason {
Ok(address) => {
interpreter.machine_mut().stack.push(address.into())?;
Expand Down
3 changes: 2 additions & 1 deletion interpreter/tests/performance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use evm_interpreter::{Capture, Etable, EtableInterpreter, ExitSucceed, Interpreter, Machine};
use evm_interpreter::interpreter::{EtableInterpreter, RunInterpreter};
use evm_interpreter::{Capture, Etable, ExitSucceed, Machine};
use std::rc::Rc;

static ETABLE: Etable<(), (), ()> = Etable::core();
Expand Down
7 changes: 4 additions & 3 deletions interpreter/tests/usability.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use evm_interpreter::interpreter::{EtableInterpreter, RunInterpreter};
use evm_interpreter::{
trap::CallCreateTrap, Capture, Context, Control, Etable, EtableInterpreter, ExitError,
ExitSucceed, Interpreter, Log, Machine, Opcode, RuntimeBackend, RuntimeBaseBackend,
RuntimeEnvironment, RuntimeState, TransactionContext,
trap::CallCreateTrap, Capture, Context, Control, Etable, ExitError, ExitSucceed, Log, Machine,
Opcode, RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment, RuntimeState,
TransactionContext,
};
use primitive_types::{H160, H256, U256};
use std::rc::Rc;
Expand Down
2 changes: 1 addition & 1 deletion jsontests/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::types::*;
use evm::standard::{Config, Etable, EtableResolver, Invoker, TransactArgs};
use evm::utils::u256_to_h256;
use evm::Capture;
use evm::{GasState, Interpreter};
use evm::{interpreter::Interpreter, GasState};
use evm_precompile::StandardPrecompileSet;
use primitive_types::U256;
use std::collections::{BTreeMap, BTreeSet};
Expand Down
Loading

0 comments on commit 92504d6

Please sign in to comment.