Skip to content

Commit

Permalink
reorg interpreter and adjust rustfmt (rust-ethereum#287)
Browse files Browse the repository at this point in the history
reorg interpreter crate
 - extract exit module from error module
 - move trap module into error module
 - add machine module
 - move memory and stack modules into machine module
 
add more rustfmt configs

```toml
imports_granularity="Crate"
group_imports = "StdExternalCrate"
```
  • Loading branch information
koushiro authored May 28, 2024
1 parent 1218997 commit 699fd9a
Show file tree
Hide file tree
Showing 49 changed files with 488 additions and 355 deletions.
27 changes: 1 addition & 26 deletions interpreter/src/error.rs → interpreter/src/error/exit.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
use crate::Opcode;
use alloc::borrow::Cow;
use core::fmt;

/// Capture represents the result of execution.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Capture<E, T> {
/// The machine has exited. It cannot be executed again.
Exit(E),
/// The machine has trapped. It is waiting for external information, and can
/// be executed again.
Trap(T),
}

impl<E, T> Capture<E, T> {
pub fn exit(self) -> Option<E> {
match self {
Self::Exit(e) => Some(e),
Self::Trap(_) => None,
}
}

pub fn trap(self) -> Option<T> {
match self {
Self::Exit(_) => None,
Self::Trap(t) => Some(t),
}
}
}
use crate::opcode::Opcode;

/// Exit result.
pub type ExitResult = Result<ExitSucceed, ExitError>;
Expand Down
30 changes: 30 additions & 0 deletions interpreter/src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
mod exit;
mod trap;

pub use self::{exit::*, trap::*};

/// Capture represents the result of execution.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Capture<E, T> {
/// The machine has exited. It cannot be executed again.
Exit(E),
/// The machine has trapped. It is waiting for external information, and can
/// be executed again.
Trap(T),
}

impl<E, T> Capture<E, T> {
pub fn exit(self) -> Option<E> {
match self {
Self::Exit(e) => Some(e),
Self::Trap(_) => None,
}
}

pub fn trap(self) -> Option<T> {
match self {
Self::Exit(_) => None,
Self::Trap(t) => Some(t),
}
}
}
153 changes: 80 additions & 73 deletions interpreter/src/trap.rs → interpreter/src/error/trap.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
//! Call and create trap handler.
use crate::utils::{h256_to_u256, u256_to_usize};
use crate::{
interpreter::Interpreter, Context, ExitError, ExitException, ExitResult, Machine, Memory,
RuntimeBackend, RuntimeState, Transfer,
};
use alloc::vec::Vec;
use core::cmp::{max, min};
use core::convert::Infallible;
use core::{
cmp::{max, min},
convert::Infallible,
};

use primitive_types::{H160, H256, U256};
use sha3::{Digest, Keccak256};

use crate::{
error::{ExitError, ExitException, ExitResult},
interpreter::Interpreter,
machine::{Machine, Memory},
runtime::{Context, RuntimeBackend, RuntimeState, Transfer},
utils::{h256_to_u256, u256_to_usize},
};

pub trait TrapConstruct<T> {
fn construct(v: T) -> Self;
}
Expand All @@ -21,72 +27,6 @@ pub trait TrapConsume<T> {
fn consume(self) -> Result<T, Self::Rest>;
}

/// Create scheme.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum CreateScheme {
/// Legacy create scheme of `CREATE`.
Legacy {
/// Caller of the create.
caller: H160,
},
/// Create scheme of `CREATE2`.
Create2 {
/// Caller of the create.
caller: H160,
/// Code hash.
code_hash: H256,
/// Salt.
salt: H256,
},
}

impl CreateScheme {
pub fn address<H: RuntimeBackend>(&self, handler: &H) -> H160 {
match self {
Self::Create2 {
caller,
code_hash,
salt,
} => {
let mut hasher = Keccak256::new();
hasher.update([0xff]);
hasher.update(&caller[..]);
hasher.update(&salt[..]);
hasher.update(&code_hash[..]);
H256::from_slice(hasher.finalize().as_slice()).into()
}
Self::Legacy { caller } => {
let nonce = handler.nonce(*caller);
let mut stream = rlp::RlpStream::new_list(2);
stream.append(caller);
stream.append(&nonce);
H256::from_slice(Keccak256::digest(&stream.out()).as_slice()).into()
}
}
}

#[must_use]
pub const fn caller(&self) -> H160 {
match self {
Self::Create2 { caller, .. } => *caller,
Self::Legacy { caller } => *caller,
}
}
}

/// Call scheme.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum CallScheme {
/// `CALL`
Call,
/// `CALLCODE`
CallCode,
/// `DELEGATECALL`
DelegateCall,
/// `STATICCALL`
StaticCall,
}

pub enum CallCreateTrap {
Create,
Create2,
Expand Down Expand Up @@ -161,6 +101,20 @@ impl CallCreateTrapData {
}
}

/// Call scheme.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum CallScheme {
/// `CALL`
Call,
/// `CALLCODE`
CallCode,
/// `DELEGATECALL`
DelegateCall,
/// `STATICCALL`
StaticCall,
}

#[derive(Clone, Debug)]
pub struct CallTrapData {
pub target: H160,
pub transfer: Option<Transfer>,
Expand Down Expand Up @@ -376,6 +330,59 @@ impl CallTrapData {
}
}

/// Create scheme.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum CreateScheme {
/// Legacy create scheme of `CREATE`.
Legacy {
/// Caller of the create call.
caller: H160,
},
/// Create scheme of `CREATE2`.
Create2 {
/// Caller of the create call.
caller: H160,
/// Code hash.
code_hash: H256,
/// Salt.
salt: H256,
},
}

impl CreateScheme {
pub fn address<H: RuntimeBackend>(&self, handler: &H) -> H160 {
match self {
Self::Create2 {
caller,
code_hash,
salt,
} => {
let mut hasher = Keccak256::new();
hasher.update([0xff]);
hasher.update(&caller[..]);
hasher.update(&salt[..]);
hasher.update(&code_hash[..]);
H256::from_slice(hasher.finalize().as_slice()).into()
}
Self::Legacy { caller } => {
let nonce = handler.nonce(*caller);
let mut stream = rlp::RlpStream::new_list(2);
stream.append(caller);
stream.append(&nonce);
H256::from_slice(Keccak256::digest(&stream.out()).as_slice()).into()
}
}
}

#[must_use]
pub const fn caller(&self) -> H160 {
match self {
Self::Create2 { caller, .. } => *caller,
Self::Legacy { caller } => *caller,
}
}
}

#[derive(Clone, Debug)]
pub struct CreateTrapData {
pub scheme: CreateScheme,
Expand Down
14 changes: 10 additions & 4 deletions interpreter/src/etable.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use core::{
marker::PhantomData,
ops::{Deref, DerefMut},
};

use crate::{
eval::*, trap::CallCreateTrap, ExitResult, GasState, Machine, Opcode, RuntimeBackend,
RuntimeEnvironment, RuntimeState, TrapConstruct,
error::{CallCreateTrap, ExitResult, TrapConstruct},
eval::*,
machine::Machine,
opcode::Opcode,
runtime::{GasState, RuntimeBackend, RuntimeEnvironment, RuntimeState},
};
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};

pub trait EtableSet {
type State;
Expand Down
5 changes: 3 additions & 2 deletions interpreter/src/eval/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::utils::I256;
use core::convert::TryInto;
use core::ops::Rem;

use primitive_types::{U256, U512};

use crate::utils::I256;

#[inline]
pub fn div(op1: U256, op2: U256) -> U256 {
if op2 == U256::zero() {
Expand Down
3 changes: 2 additions & 1 deletion interpreter/src/eval/bitwise.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::utils::{Sign, I256};
use primitive_types::U256;

use crate::utils::{Sign, I256};

#[inline]
pub fn slt(op1: U256, op2: U256) -> U256 {
let op1: I256 = op1.into();
Expand Down
11 changes: 8 additions & 3 deletions interpreter/src/eval/misc.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use super::Control;
use crate::utils::u256_to_h256;
use crate::{ExitError, ExitException, ExitFatal, ExitSucceed, Machine};
use core::cmp::min;

use primitive_types::{H256, U256};

use crate::{
error::{ExitError, ExitException, ExitFatal, ExitSucceed},
etable::Control,
machine::Machine,
utils::u256_to_h256,
};

#[inline]
pub fn codesize<S, Tr>(state: &mut Machine<S>) -> Control<Tr> {
let stack = &mut state.stack;
Expand Down
13 changes: 9 additions & 4 deletions interpreter/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ mod bitwise;
mod misc;
mod system;

use crate::{
trap::CallCreateTrap, Control, ExitException, ExitSucceed, GasState, Machine, Opcode,
RuntimeBackend, RuntimeEnvironment, RuntimeState, TrapConstruct,
};
use core::ops::{BitAnd, BitOr, BitXor};

use primitive_types::{H256, U256};

use crate::{
error::{CallCreateTrap, ExitException, ExitSucceed, TrapConstruct},
etable::Control,
machine::Machine,
opcode::Opcode,
runtime::{GasState, RuntimeBackend, RuntimeEnvironment, RuntimeState},
};

pub fn eval_pass<S, H, Tr>(
_machine: &mut Machine<S>,
_handle: &mut H,
Expand Down
13 changes: 8 additions & 5 deletions interpreter/src/eval/system.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use super::Control;
use crate::{
ExitException, ExitFatal, ExitSucceed, GasState, Log, Machine, RuntimeBackend,
RuntimeEnvironment, RuntimeState, Transfer,
};
use alloc::vec::Vec;

use primitive_types::{H256, U256};
use sha3::{Digest, Keccak256};

use crate::{
error::{ExitException, ExitFatal, ExitSucceed},
etable::Control,
machine::Machine,
runtime::{GasState, Log, RuntimeBackend, RuntimeEnvironment, RuntimeState, Transfer},
};

pub fn sha3<S: AsRef<RuntimeState>, Tr>(machine: &mut Machine<S>) -> Control<Tr> {
pop_u256!(machine, from, len);

Expand Down
13 changes: 8 additions & 5 deletions interpreter/src/interpreter/etable.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::interpreter::{Interpreter, RunInterpreter, StepInterpreter};
use crate::{
Capture, Control, EtableSet, ExitError, ExitException, ExitFatal, ExitResult, ExitSucceed,
Machine, Opcode, Stack, Valids,
};
use alloc::vec::Vec;
use core::ops::{Deref, DerefMut};

use crate::{
error::{Capture, ExitError, ExitException, ExitFatal, ExitResult, ExitSucceed},
etable::{Control, EtableSet},
interpreter::{valids::Valids, Interpreter, RunInterpreter, StepInterpreter},
machine::{Machine, Stack},
opcode::Opcode,
};

pub struct EtableInterpreter<'etable, ES: EtableSet> {
valids: Valids,
position: usize,
Expand Down
10 changes: 7 additions & 3 deletions interpreter/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
mod etable;
mod valids;

pub use self::etable::EtableInterpreter;

use crate::{Capture, ExitResult, Machine};
use alloc::vec::Vec;

pub use self::etable::EtableInterpreter;
use crate::{
error::{Capture, ExitResult},
machine::Machine,
};

pub trait Interpreter {
type State;

Expand Down
Loading

0 comments on commit 699fd9a

Please sign in to comment.