Skip to content

Commit

Permalink
chore: move some files. cleanup comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Apr 9, 2024
1 parent eeeb798 commit 4c60857
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 106 deletions.
2 changes: 1 addition & 1 deletion bins/revm-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
bytes = "1.6"
hex = "0.4"
revm = { path = "../../crates/revm", version = "8.0.0",default-features=false}
revm = { path = "../../crates/revm", version = "8.0.0", default-features=false }
microbench = "0.5"
alloy-sol-macro = "0.7.0"
alloy-sol-types = "0.7.0"
Expand Down
4 changes: 1 addition & 3 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ pub fn find_all_json_tests(path: &Path) -> Vec<PathBuf> {
fn skip_test(path: &Path) -> bool {
let path_str = path.to_str().expect("Path is not valid UTF-8");
let name = path.file_name().unwrap().to_str().unwrap();
if path_str.contains("stRandom") {
return true;
}

matches!(
name,
// funky test with `bigint 0x00` value in json :) not possible to happen on mainnet and require
Expand Down
3 changes: 3 additions & 0 deletions crates/interpreter/src/function_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ pub struct FunctionReturnFrame {
}

impl FunctionReturnFrame {
/// Return new function frame.
pub fn new(idx: usize, pc: usize) -> Self {
Self { idx, pc }
}
}

/// Function Stack
#[derive(Debug, Default)]
pub struct FunctionStack {
pub return_stack: Vec<FunctionReturnFrame>,
pub current_code_idx: usize,
}

impl FunctionStack {
/// Returns new function stack.
pub fn new() -> Self {
Self {
return_stack: Vec::new(),
Expand Down
14 changes: 12 additions & 2 deletions crates/interpreter/src/gas/calc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use revm_primitives::Bytes;

use super::constants::*;
use crate::{
primitives::{
Expand Down Expand Up @@ -356,10 +358,18 @@ pub fn validate_initial_tx_gas(
input: &[u8],
is_create: bool,
access_list: &[(Address, Vec<U256>)],
initcodes: &[Bytes],
) -> u64 {
let mut initial_gas = 0;
let zero_data_len = input.iter().filter(|v| **v == 0).count() as u64;
let non_zero_data_len = input.len() as u64 - zero_data_len;
let mut zero_data_len = input.iter().filter(|v| **v == 0).count() as u64;
let mut non_zero_data_len = input.len() as u64 - zero_data_len;

// Enabling of initcode is checked in `validate_env` handler.
for initcode in initcodes {
let zeros = initcode.iter().filter(|v| **v == 0).count() as u64;
zero_data_len += zeros;
non_zero_data_len += initcode.len() as u64 - zeros;
}

// initdate stipend
initial_gas += zero_data_len * TRANSACTION_ZERO_DATA;
Expand Down
52 changes: 0 additions & 52 deletions crates/interpreter/src/inner_models.rs

This file was deleted.

8 changes: 7 additions & 1 deletion crates/interpreter/src/instructions/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ pub fn txcreate<H: Host + ?Sized>(interpreter: &mut Interpreter, host: &mut H) {
};

// fetch initcode, if not found push ZERO.
let Some(initcode) = host.env().tx.eof_initcodes.get(&tx_initcode_hash).cloned() else {
let Some(initcode) = host
.env()
.tx
.eof_initcodes_hashed
.get(&tx_initcode_hash)
.cloned()
else {
push!(interpreter, U256::ZERO);
return;
};
Expand Down
19 changes: 5 additions & 14 deletions crates/interpreter/src/interpreter/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,8 @@ pub fn validate_raw_eof(bytecode: Bytes) -> Result<Eof, EofError> {
}

/// Validate Eof structures.
///
/// do perf test on:
/// max eof containers
/// max depth of containers.
/// bytecode iteration.
pub fn validate_eof(eof: &Eof) -> Result<(), EofError> {
// clone is cheat as it is Bytes and a header.
// clone is cheap as it is Bytes and a header.
let mut queue = vec![eof.clone()];

while let Some(eof) = queue.pop() {
Expand Down Expand Up @@ -316,8 +311,6 @@ pub fn validate_eof_code(

let this_instruction = &mut jumps[i];

//println!("next b: {next_biggest}, next s: {next_smallest} terminal: {is_after_termination} opcode: {}",opcode.name);

// update biggest/smallest values for next instruction only if it is not after termination.
if !is_after_termination {
this_instruction.smallest = core::cmp::min(this_instruction.smallest, next_smallest);
Expand Down Expand Up @@ -497,10 +490,9 @@ pub fn validate_eof_code(
return Err(EofValidationError::StackUnderflow);
}

//println!("stack_io_diff: {}", stack_io_diff);
next_smallest = this_instruction.smallest + stack_io_diff;
next_biggest = this_instruction.biggest + stack_io_diff;
//println!("absolute_jumpdest: {absolute_jumpdest:?}");

// check if jumpdest are correct and mark forward jumps.
for absolute_jump in absolute_jumpdest {
if absolute_jump < 0 {
Expand All @@ -524,7 +516,6 @@ pub fn validate_eof_code(
target_jump.is_jumpdest = true;

if absolute_jump <= i {
//println!("JUMP: {i} -> {target_jump:?}");
// backward jumps should have same smallest and biggest stack items.
if target_jump.biggest != next_biggest {
// wrong jumpdest.
Expand Down Expand Up @@ -572,23 +563,23 @@ mod test {

#[test]
fn test1() {
//result:Result { result: false, exception: Some("EOF_ConflictingStackHeight") }
// result:Result { result: false, exception: Some("EOF_ConflictingStackHeight") }
let err =
validate_raw_eof(hex!("ef0001010004020001000704000000008000016000e200fffc00").into());
assert!(err.is_err(), "{err:#?}");
}

#[test]
fn test2() {
//result:Result { result: false, exception: Some("EOF_InvalidNumberOfOutputs") }
// result:Result { result: false, exception: Some("EOF_InvalidNumberOfOutputs") }
let err =
validate_raw_eof(hex!("ef000101000c02000300040004000204000000008000020002000100010001e30001005fe500025fe4").into());
assert!(err.is_ok(), "{err:#?}");
}

#[test]
fn test3() {
//result:Result { result: false, exception: Some("EOF_InvalidNumberOfOutputs") }
// result:Result { result: false, exception: Some("EOF_InvalidNumberOfOutputs") }
let err =
validate_raw_eof(hex!("ef000101000c02000300040008000304000000008000020002000503010003e30001005f5f5f5f5fe500025050e4").into());
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/interpreter/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Stack {
(pop1, pop2, pop3, pop4)
}

/// Pops 4 values from the stack.
/// Pops 5 values from the stack.
///
/// # Safety
///
Expand Down
67 changes: 67 additions & 0 deletions crates/interpreter/src/interpreter_action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
mod call_inputs;
mod call_outcome;
mod create_inputs;
mod create_outcome;
mod eof_create_inputs;
mod eof_create_outcome;

pub use call_inputs::{CallInputs, CallScheme, TransferValue};
pub use call_outcome::CallOutcome;
pub use create_inputs::{CreateInputs, CreateScheme};
pub use create_outcome::CreateOutcome;
pub use eof_create_inputs::EOFCreateInput;
pub use eof_create_outcome::EOFCreateOutcome;

use crate::InterpreterResult;
use std::boxed::Box;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum InterpreterAction {
/// CALL, CALLCODE, DELEGATECALL, STATICCALL
/// or EOF EXT instuction called.
Call { inputs: Box<CallInputs> },
/// CREATE or CREATE2 instruction called.
Create { inputs: Box<CreateInputs> },
/// EOF CREATE instruction called.
EOFCreate { inputs: Box<EOFCreateInput> },
/// Interpreter finished execution.
Return { result: InterpreterResult },
/// No action
#[default]
None,
}

impl InterpreterAction {
/// Returns true if action is call.
pub fn is_call(&self) -> bool {
matches!(self, InterpreterAction::Call { .. })
}

/// Returns true if action is create.
pub fn is_create(&self) -> bool {
matches!(self, InterpreterAction::Create { .. })
}

/// Returns true if action is return.
pub fn is_return(&self) -> bool {
matches!(self, InterpreterAction::Return { .. })
}

/// Returns true if action is none.
pub fn is_none(&self) -> bool {
matches!(self, InterpreterAction::None)
}

/// Returns true if action is some.
pub fn is_some(&self) -> bool {
!self.is_none()
}

/// Returns result if action is return.
pub fn into_result_return(self) -> Option<InterpreterResult> {
match self {
InterpreterAction::Return { result } => Some(result),
_ => None,
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ pub struct EOFCreateOutcome {
}

impl EOFCreateOutcome {
/// Constructs a new `CreateOutcome`.
/// Constructs a new [`EOFCreateOutcome`].
///
/// # Arguments
///
/// * `result` - An `InterpreterResult` representing the result of the interpreter operation.
/// * `address` - An optional `Address` associated with the create operation.
/// * `return_memory_range` - The memory range that Revert bytes are going to be written.
///
/// # Returns
///
/// A new `CreateOutcome` instance.
/// A new [`EOFCreateOutcome`] instance.
pub fn new(
result: InterpreterResult,
address: Address,
Expand All @@ -40,7 +41,7 @@ impl EOFCreateOutcome {
}
}

/// Retrieves a reference to the `InstructionResult` from the `InterpreterResult`.
/// Retrieves a reference to the [`InstructionResult`] from the [`InterpreterResult`].
///
/// This method provides access to the `InstructionResult` which represents the
/// outcome of the instruction execution. It encapsulates the result information
Expand Down
20 changes: 6 additions & 14 deletions crates/interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,29 @@ use serde_json as _;
#[cfg(test)]
use walkdir as _;

mod call_inputs;
mod call_outcome;
mod create_inputs;
mod create_outcome;
mod eof_create_inputs;
mod eof_create_outcome;
mod function_stack;
pub mod gas;
mod host;
mod instruction_result;
pub mod instructions;
pub mod interpreter;
pub mod interpreter_action;
pub mod opcode;

// Reexport primary types.
pub use call_inputs::{CallInputs, CallScheme, TransferValue};
pub use call_outcome::CallOutcome;
pub use create_inputs::{CreateInputs, CreateScheme};
pub use create_outcome::CreateOutcome;
pub use eof_create_inputs::EOFCreateInput;
pub use eof_create_outcome::EOFCreateOutcome;
pub use function_stack::{FunctionReturnFrame, FunctionStack};
pub use gas::Gas;
pub use host::{DummyHost, Host, LoadAccountResult, SStoreResult, SelfDestructResult};
pub use instruction_result::*;
pub use opcode::{Instruction, OpCode, OPCODE_INFO_JUMPTABLE};

pub use interpreter::{
analysis, next_multiple_of_32, Contract, Interpreter, InterpreterAction, InterpreterResult,
SharedMemory, Stack, EMPTY_SHARED_MEMORY, STACK_LIMIT,
};
pub use interpreter_action::{
CallInputs, CallOutcome, CallScheme, CreateInputs, CreateOutcome, CreateScheme, EOFCreateInput,
EOFCreateOutcome, TransferValue,
};
pub use opcode::{Instruction, OpCode, OPCODE_INFO_JUMPTABLE};
pub use primitives::{MAX_CODE_SIZE, MAX_INITCODE_SIZE};

#[doc(hidden)]
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Bytecode {
///
/// # Safety
///
/// Bytecode need to end with STOP (0x00) opcode as checked bytecode assumes
/// Bytecode needs to end with STOP (0x00) opcode as checked bytecode assumes
/// that it is safe to iterate over bytecode without checking lengths.
pub unsafe fn new_analyzed(
bytecode: Bytes,
Expand Down
Loading

0 comments on commit 4c60857

Please sign in to comment.