diff --git a/crates/interpreter/src/gas.rs b/crates/interpreter/src/gas.rs index 79d6f3905d..350c1ecef1 100644 --- a/crates/interpreter/src/gas.rs +++ b/crates/interpreter/src/gas.rs @@ -1,5 +1,7 @@ -pub mod calc; -pub mod constants; +//! EVM gas calculation utilities. + +mod calc; +mod constants; pub use calc::*; pub use constants::*; diff --git a/crates/interpreter/src/host.rs b/crates/interpreter/src/host.rs index 57afa405aa..54607dff71 100644 --- a/crates/interpreter/src/host.rs +++ b/crates/interpreter/src/host.rs @@ -10,48 +10,67 @@ mod dummy; /// EVM context host. pub trait Host { + /// Called before the interpreter executes an instruction. fn step(&mut self, interpreter: &mut Interpreter) -> InstructionResult; + + /// Called after the interpreter executes an instruction. fn step_end( &mut self, interpreter: &mut Interpreter, ret: InstructionResult, ) -> InstructionResult; + /// Returns a mutable reference to the environment. fn env(&mut self) -> &mut Env; - /// load account. Returns (is_cold,is_new_account) + /// Load an account. + /// + /// Returns (is_cold, is_new_account) fn load_account(&mut self, address: Address) -> Option<(bool, bool)>; - /// Get environmental block hash. + + /// Get the block hash of the given block `number`. fn block_hash(&mut self, number: U256) -> Option; - /// Get balance of address and if account is cold loaded. + + /// Get balance of `address` and if the account is cold. fn balance(&mut self, address: Address) -> Option<(U256, bool)>; - /// Get code of address and if account is cold loaded. + + /// Get code of `address` and if the account is cold. fn code(&mut self, address: Address) -> Option<(Bytecode, bool)>; - /// Get code hash of address and if account is cold loaded. + + /// Get code hash of `address` and if the account is cold. fn code_hash(&mut self, address: Address) -> Option<(B256, bool)>; - /// Get storage value of address at index and if account is cold loaded. + + /// Get storage value of `address` at `index` and if the account is cold. fn sload(&mut self, address: Address, index: U256) -> Option<(U256, bool)>; + /// Set storage value of account address at index. - /// Returns (original, present, new, sis_cold) + /// + /// Returns (original, present, new, is_cold). fn sstore( &mut self, address: Address, index: U256, value: U256, ) -> Option<(U256, U256, U256, bool)>; - /// Get the transient storage value of address at index. + + /// Get the transient storage value of `address` at `index`. fn tload(&mut self, address: Address, index: U256) -> U256; - /// Set the transient storage value of address at index. + + /// Set the transient storage value of `address` at `index`. fn tstore(&mut self, address: Address, index: U256, value: U256); - /// Create a log owned by address with given topics and data. + + /// Emit a log owned by `address` with given `topics` and `data`. fn log(&mut self, address: Address, topics: Vec, data: Bytes); - /// Mark an address to be deleted, with funds transferred to target. - fn selfdestruct(&mut self, address: Address, target: Address) -> Option; + + /// Invoke a call operation. + fn call(&mut self, input: &mut CallInputs) -> (InstructionResult, Gas, Bytes); + /// Invoke a create operation. fn create( &mut self, inputs: &mut CreateInputs, ) -> (InstructionResult, Option
, Gas, Bytes); - /// Invoke a call operation. - fn call(&mut self, input: &mut CallInputs) -> (InstructionResult, Gas, Bytes); + + /// Mark `address` to be deleted, with funds transferred to `target`. + fn selfdestruct(&mut self, address: Address, target: Address) -> Option; } diff --git a/crates/interpreter/src/host/dummy.rs b/crates/interpreter/src/host/dummy.rs index 72b7699da2..5b58bfbb2d 100644 --- a/crates/interpreter/src/host/dummy.rs +++ b/crates/interpreter/src/host/dummy.rs @@ -5,6 +5,7 @@ use crate::{ }; use alloc::vec::Vec; +/// A dummy [Host] implementation. pub struct DummyHost { pub env: Env, pub storage: HashMap, diff --git a/crates/interpreter/src/inner_models.rs b/crates/interpreter/src/inner_models.rs index 614ea567d0..c637ec6017 100644 --- a/crates/interpreter/src/inner_models.rs +++ b/crates/interpreter/src/inner_models.rs @@ -14,16 +14,22 @@ pub struct CallInputs { pub gas_limit: u64, /// The context of the call. pub context: CallContext, - /// Is static call + /// Whether this is a static call. pub is_static: bool, } +/// Inputs for a create call. #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct CreateInputs { + /// Caller address of the EVM. pub caller: Address, + /// The create scheme. pub scheme: CreateScheme, + /// The value to transfer. pub value: U256, + /// The init code of the contract. pub init_code: Bytes, + /// The gas limit of the call. pub gas_limit: u64, } @@ -41,13 +47,13 @@ pub enum CallScheme { StaticCall, } -/// CallContext of the runtime. +/// Context of a runtime call. #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct CallContext { /// Execution address. pub address: Address, - /// Caller of the EVM. + /// Caller address of the EVM. pub caller: Address, /// The address the contract code was loaded from, if any. pub code_address: Address, @@ -73,14 +79,15 @@ impl Default for CallContext { #[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Transfer { - /// Source address. + /// The source address. pub source: Address, - /// Target address. + /// The target address. pub target: Address, - /// Transfer value. + /// The transfer value. pub value: U256, } +/// Result of a call that resulted in a self destruct. #[derive(Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct SelfDestructResult { diff --git a/crates/interpreter/src/instructions.rs b/crates/interpreter/src/instructions.rs index 5088aa4dd8..b313f8421d 100644 --- a/crates/interpreter/src/instructions.rs +++ b/crates/interpreter/src/instructions.rs @@ -1,5 +1,7 @@ +//! EVM opcode implementations. + #[macro_use] -pub mod macros; +mod macros; pub mod arithmetic; pub mod bitwise; diff --git a/crates/interpreter/src/instructions/macros.rs b/crates/interpreter/src/instructions/macros.rs index 714a83368b..826a6c0be6 100644 --- a/crates/interpreter/src/instructions/macros.rs +++ b/crates/interpreter/src/instructions/macros.rs @@ -53,7 +53,7 @@ macro_rules! gas_or_fail { macro_rules! memory_resize { ($interp:expr, $offset:expr, $len:expr) => { if let Some(new_size) = - crate::interpreter::memory::next_multiple_of_32($offset.saturating_add($len)) + crate::interpreter::next_multiple_of_32($offset.saturating_add($len)) { #[cfg(feature = "memory_limit")] if new_size > ($interp.memory_limit as usize) { diff --git a/crates/interpreter/src/interpreter.rs b/crates/interpreter/src/interpreter.rs index d13bd1998a..f274b5cb54 100644 --- a/crates/interpreter/src/interpreter.rs +++ b/crates/interpreter/src/interpreter.rs @@ -1,6 +1,6 @@ pub mod analysis; mod contract; -pub mod memory; +mod memory; mod stack; use crate::primitives::{Bytes, Spec}; @@ -8,10 +8,8 @@ use crate::{alloc::boxed::Box, opcode::eval, Gas, Host, InstructionResult}; pub use analysis::BytecodeLocked; pub use contract::Contract; -pub use memory::Memory; -pub use stack::{Stack, STACK_LIMIT}; - -pub const CALL_STACK_LIMIT: u64 = 1024; +pub use memory::{next_multiple_of_32, Memory}; +pub use stack::Stack; /// EIP-170: Contract code size limit /// diff --git a/crates/interpreter/src/interpreter/analysis.rs b/crates/interpreter/src/interpreter/analysis.rs index d26fa85676..5778470fef 100644 --- a/crates/interpreter/src/interpreter/analysis.rs +++ b/crates/interpreter/src/interpreter/analysis.rs @@ -58,6 +58,7 @@ fn analyze(code: &[u8]) -> JumpMap { JumpMap(Arc::new(jumps)) } +/// An analyzed bytecode. #[derive(Clone)] pub struct BytecodeLocked { bytecode: Bytes, diff --git a/crates/interpreter/src/interpreter/contract.rs b/crates/interpreter/src/interpreter/contract.rs index bc7a248097..a5896cc865 100644 --- a/crates/interpreter/src/interpreter/contract.rs +++ b/crates/interpreter/src/interpreter/contract.rs @@ -2,6 +2,7 @@ use super::analysis::{to_analysed, BytecodeLocked}; use crate::primitives::{Address, Bytecode, Bytes, Env, TransactTo, B256, U256}; use crate::CallContext; +/// EVM contract information. #[derive(Clone, Debug, Default)] pub struct Contract { /// Contracts data diff --git a/crates/interpreter/src/interpreter/memory.rs b/crates/interpreter/src/interpreter/memory.rs index aa3a43fdb0..f0c15bcd2d 100644 --- a/crates/interpreter/src/interpreter/memory.rs +++ b/crates/interpreter/src/interpreter/memory.rs @@ -186,15 +186,14 @@ impl Memory { /// Rounds up `x` to the closest multiple of 32. If `x % 32 == 0` then `x` is returned. #[inline] -pub(crate) fn next_multiple_of_32(x: usize) -> Option { +pub fn next_multiple_of_32(x: usize) -> Option { let r = x.bitand(31).not().wrapping_add(1).bitand(31); x.checked_add(r) } #[cfg(test)] mod tests { - use super::next_multiple_of_32; - use crate::Memory; + use super::*; #[test] fn test_copy() { diff --git a/crates/interpreter/src/interpreter/stack.rs b/crates/interpreter/src/interpreter/stack.rs index 0a3f8a2edb..d79f6c9e1c 100644 --- a/crates/interpreter/src/interpreter/stack.rs +++ b/crates/interpreter/src/interpreter/stack.rs @@ -5,7 +5,7 @@ use crate::{ use alloc::vec::Vec; use core::fmt; -/// The EVM stack limit, in number of items. +/// EVM interpreter stack limit. pub const STACK_LIMIT: usize = 1024; /// EVM stack. diff --git a/crates/interpreter/src/lib.rs b/crates/interpreter/src/lib.rs index a55265d791..ebf97afbcd 100644 --- a/crates/interpreter/src/lib.rs +++ b/crates/interpreter/src/lib.rs @@ -1,3 +1,7 @@ +//! # revm-interpreter +//! +//! REVM Interpreter. + #![cfg_attr(not(feature = "std"), no_std)] #![warn(unused_crate_dependencies)] @@ -8,8 +12,8 @@ mod macros; pub mod gas; mod host; -pub mod inner_models; -pub mod instruction_result; +mod inner_models; +mod instruction_result; pub mod instructions; mod interpreter; @@ -19,12 +23,11 @@ pub(crate) const USE_GAS: bool = !cfg!(feature = "no_gas_measuring"); pub use gas::Gas; pub use host::{DummyHost, Host}; pub use inner_models::*; -pub use instruction_result::InstructionResult; +pub use instruction_result::*; pub use instructions::{opcode, Instruction, OpCode, OPCODE_JUMPMAP}; pub use interpreter::{ - analysis, BytecodeLocked, Contract, Interpreter, Memory, Stack, CALL_STACK_LIMIT, - MAX_CODE_SIZE, MAX_INITCODE_SIZE, + analysis, BytecodeLocked, Contract, Interpreter, Memory, Stack, MAX_CODE_SIZE, + MAX_INITCODE_SIZE, }; - -#[doc(inline)] +#[doc(hidden)] pub use revm_primitives as primitives; diff --git a/crates/precompile/Cargo.toml b/crates/precompile/Cargo.toml index 99d8183afa..46e9aea3ea 100644 --- a/crates/precompile/Cargo.toml +++ b/crates/precompile/Cargo.toml @@ -8,9 +8,6 @@ name = "revm-precompile" repository = "https://github.com/bluealloy/revm" version = "2.2.0" -# Don't need to run build script outside of this repo -exclude = ["build.rs", "src/blob/kzg_settings/*.txt"] - [dependencies] revm-primitives = { path = "../primitives", version = "1.3.0", default-features = false } bn = { package = "substrate-bn", version = "0.6", default-features = false } @@ -44,7 +41,6 @@ std = [ optimism = ["revm-primitives/optimism"] - # This library may not work on all no_std platforms as they depend on C libraries. # Enables the KZG point evaluation precompile. diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 36c3d88f9c..4fdef0595b 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -1,4 +1,8 @@ -#![no_std] +//! # revm-precompile +//! +//! Implementations of EVM precompiled contracts. + +#![cfg_attr(not(feature = "std"), no_std)] #![warn(unused_crate_dependencies)] #[macro_use] @@ -16,12 +20,12 @@ mod secp256k1; use alloc::{boxed::Box, vec::Vec}; use core::fmt; use once_cell::race::OnceBox; -pub use primitives::{ +#[doc(hidden)] +pub use revm_primitives as primitives; +pub use revm_primitives::{ precompile::{PrecompileError as Error, *}, Bytes, HashMap, }; -#[doc(inline)] -pub use revm_primitives as primitives; pub type Address = [u8; 20]; pub type B256 = [u8; 32]; diff --git a/crates/precompile/src/secp256k1.rs b/crates/precompile/src/secp256k1.rs index f2408a7846..dc74737e11 100644 --- a/crates/precompile/src/secp256k1.rs +++ b/crates/precompile/src/secp256k1.rs @@ -1,4 +1,6 @@ use crate::{Error, Precompile, PrecompileAddress, PrecompileResult, StandardPrecompileFn}; +use alloc::vec::Vec; +use core::cmp::min; pub const ECRECOVER: PrecompileAddress = PrecompileAddress( crate::u64_to_address(1), @@ -61,9 +63,6 @@ mod secp256k1 { } fn ec_recover_run(i: &[u8], target_gas: u64) -> PrecompileResult { - use alloc::vec::Vec; - use core::cmp::min; - const ECRECOVER_BASE: u64 = 3_000; if ECRECOVER_BASE > target_gas { diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 640a5c045a..2aa259b445 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -9,6 +9,9 @@ repository = "https://github.com/bluealloy/revm" version = "1.3.0" readme = "../../README.md" +# Don't need to run build script outside of this repo +exclude = ["build.rs", "src/kzg/*.txt"] + [dependencies] alloy-primitives = { version = "0.4", default-features = false, features = [ "rlp", diff --git a/crates/primitives/src/constants.rs b/crates/primitives/src/constants.rs index bbbf7c776d..c57045b656 100644 --- a/crates/primitives/src/constants.rs +++ b/crates/primitives/src/constants.rs @@ -1,10 +1,5 @@ use crate::Address; -/// Interpreter stack limit -pub const STACK_LIMIT: u64 = 1024; -/// EVM call stack limit -pub const CALL_STACK_LIMIT: u64 = 1024; - /// EIP-170: Contract code size limit /// By default limit is 0x6000 (~25kb) pub const MAX_CODE_SIZE: usize = 0x6000; diff --git a/crates/primitives/src/db.rs b/crates/primitives/src/db.rs index d6851b6ecb..b0c667306c 100644 --- a/crates/primitives/src/db.rs +++ b/crates/primitives/src/db.rs @@ -34,8 +34,10 @@ impl From for WrapDatabaseRef { } } +/// EVM database commit interface. #[auto_impl(&mut, Box)] pub trait DatabaseCommit { + /// Commit changes to the database. fn commit(&mut self, changes: Map); } diff --git a/crates/primitives/src/env.rs b/crates/primitives/src/env.rs index 1c64b735fb..15a8002f13 100644 --- a/crates/primitives/src/env.rs +++ b/crates/primitives/src/env.rs @@ -67,7 +67,7 @@ pub struct BlobExcessGasAndPrice { } impl BlobExcessGasAndPrice { - /// Takes excess blob gas and calculated blob fee with [`calc_blob_fee`] + /// Creates a new instance by calculating the blob gas price with [`calc_blob_gasprice`]. pub fn new(excess_blob_gas: u64) -> Self { let blob_gasprice = calc_blob_gasprice(excess_blob_gas); Self { diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 00cda698d4..1da9c0ba9f 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -1,15 +1,19 @@ +//! # revm-primitives +//! +//! EVM primitive types. + #![cfg_attr(not(feature = "std"), no_std)] #![warn(unused_crate_dependencies)] extern crate alloc; -pub mod bytecode; -pub mod constants; +mod bytecode; +mod constants; pub mod db; pub mod env; #[cfg(feature = "c-kzg")] pub mod kzg; -pub mod log; +mod log; pub mod precompile; pub mod result; pub mod specification; @@ -27,7 +31,7 @@ pub use env::*; pub use hashbrown::{hash_map, hash_set, HashMap, HashSet}; #[cfg(feature = "c-kzg")] pub use kzg::{EnvKzgSettings, KzgSettings}; -pub use log::Log; +pub use log::*; pub use precompile::*; pub use result::*; pub use specification::*; diff --git a/crates/primitives/src/result.rs b/crates/primitives/src/result.rs index e5f230099c..8c0de46f54 100644 --- a/crates/primitives/src/result.rs +++ b/crates/primitives/src/result.rs @@ -17,6 +17,7 @@ pub struct ResultAndState { pub state: State, } +/// Result of a transaction execution. #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum ExecutionResult { @@ -93,6 +94,7 @@ impl ExecutionResult { } } +/// Output of a transaction execution. #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum Output { @@ -118,11 +120,15 @@ impl Output { } } +/// EVM error. #[derive(Debug, Copy, Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum EVMError { + /// Transaction validation error. Transaction(InvalidTransaction), + /// Header validation error. Header(InvalidHeader), + /// Database error. Database(DBError), } @@ -145,6 +151,7 @@ impl From for EVMError { } } +/// Transaction validation error. #[derive(Debug, Copy, Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum InvalidTransaction { @@ -216,7 +223,7 @@ impl From for EVMError { } } -/// Errors related to misconfiguration of the `BlockEnv` +/// Errors related to misconfiguration of a [`BlockEnv`]. #[derive(Debug, Copy, Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum InvalidHeader { diff --git a/crates/primitives/src/utilities.rs b/crates/primitives/src/utilities.rs index 4d2ee7ff0d..1d04a358e3 100644 --- a/crates/primitives/src/utilities.rs +++ b/crates/primitives/src/utilities.rs @@ -22,15 +22,17 @@ pub fn create2_address(caller: Address, code_hash: B256, salt: U256) -> Address /// Calculates the `excess_blob_gas` from the parent header's `blob_gas_used` and `excess_blob_gas`. /// -/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers). +/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers +/// (`calc_excess_blob_gas`). #[inline] pub fn calc_excess_blob_gas(parent_excess_blob_gas: u64, parent_blob_gas_used: u64) -> u64 { (parent_excess_blob_gas + parent_blob_gas_used).saturating_sub(TARGET_BLOB_GAS_PER_BLOCK) } -/// Calculates the blob gasprice from the header's excess blob gas field. +/// Calculates the blob gas price from the header's excess blob gas field. /// -/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers). +/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers) +/// (`get_blob_gasprice`). #[inline] pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u128 { fake_exponential( @@ -44,11 +46,12 @@ pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u128 { /// /// This is used to calculate the blob price. /// -/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers). +/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers) +/// (`fake_exponential`). /// -/// # Panic +/// # Panics /// -/// Panics if `denominator` is zero. +/// This function panics if `denominator` is zero. #[inline] pub fn fake_exponential(factor: u64, numerator: u64, denominator: u64) -> u128 { assert_ne!(denominator, 0, "attempt to divide by zero"); diff --git a/crates/revm/src/db.rs b/crates/revm/src/db.rs index 72f35cc848..9d6494701e 100644 --- a/crates/revm/src/db.rs +++ b/crates/revm/src/db.rs @@ -1,3 +1,5 @@ +//! [Database] implementations. + pub mod emptydb; #[cfg(feature = "ethersdb")] pub mod ethersdb; diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 577b219e3f..d8ee88617a 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -1,8 +1,8 @@ use crate::handler::Handler; use crate::interpreter::{ - analysis::to_analysed, gas, instruction_result::SuccessOrHalt, return_ok, CallContext, - CallInputs, CallScheme, Contract, CreateInputs, CreateScheme, Gas, Host, InstructionResult, - Interpreter, SelfDestructResult, Transfer, CALL_STACK_LIMIT, + analysis::to_analysed, gas, return_ok, CallContext, CallInputs, CallScheme, Contract, + CreateInputs, CreateScheme, Gas, Host, InstructionResult, Interpreter, SelfDestructResult, + SuccessOrHalt, Transfer, }; use crate::journaled_state::{is_precompile, JournalCheckpoint}; use crate::primitives::{ @@ -21,6 +21,9 @@ use revm_precompile::{Precompile, Precompiles}; #[cfg(feature = "optimism")] use crate::optimism; +/// EVM call stack limit. +pub const CALL_STACK_LIMIT: u64 = 1024; + pub struct EVMData<'a, DB: Database> { pub env: &'a mut Env, pub journaled_state: JournaledState, diff --git a/crates/revm/src/inspector.rs b/crates/revm/src/inspector.rs index b1d20b9d70..f66cbfcb60 100644 --- a/crates/revm/src/inspector.rs +++ b/crates/revm/src/inspector.rs @@ -1,41 +1,40 @@ use crate::evm_impl::EVMData; use crate::interpreter::{CallInputs, CreateInputs, Gas, InstructionResult, Interpreter}; use crate::primitives::{db::Database, Address, Bytes, B256, U256}; - use auto_impl::auto_impl; #[cfg(feature = "std")] mod customprinter; +#[cfg(all(feature = "std", feature = "serde"))] +mod eip3155; mod gas; mod noop; -#[cfg(all(feature = "std", feature = "serde"))] -mod tracer_eip3155; -/// All Inspectors implementations that revm has. +/// [Inspector] implementations. pub mod inspectors { #[cfg(feature = "std")] - #[doc(inline)] pub use super::customprinter::CustomPrintTracer; - #[doc(inline)] + #[cfg(all(feature = "std", feature = "serde"))] + pub use super::eip3155::TracerEip3155; pub use super::gas::GasInspector; - #[doc(inline)] pub use super::noop::NoOpInspector; - #[cfg(all(feature = "std", feature = "serde"))] - #[doc(inline)] - pub use super::tracer_eip3155::TracerEip3155; } +/// EVM [Interpreter] callbacks. #[auto_impl(&mut, Box)] pub trait Inspector { - /// Called Before the interpreter is initialized. + /// Called before the interpreter is initialized. /// /// If anything other than [InstructionResult::Continue] is returned then execution of the interpreter is /// skipped. + #[inline] fn initialize_interp( &mut self, - _interp: &mut Interpreter, - _data: &mut EVMData<'_, DB>, + interp: &mut Interpreter, + data: &mut EVMData<'_, DB>, ) -> InstructionResult { + let _ = interp; + let _ = data; InstructionResult::Continue } @@ -47,44 +46,55 @@ pub trait Inspector { /// # Example /// /// To get the current opcode, use `interp.current_opcode()`. - fn step( - &mut self, - _interp: &mut Interpreter, - _data: &mut EVMData<'_, DB>, - ) -> InstructionResult { + #[inline] + fn step(&mut self, interp: &mut Interpreter, data: &mut EVMData<'_, DB>) -> InstructionResult { + let _ = interp; + let _ = data; InstructionResult::Continue } /// Called when a log is emitted. + #[inline] fn log( &mut self, - _evm_data: &mut EVMData<'_, DB>, - _address: &Address, - _topics: &[B256], - _data: &Bytes, + evm_data: &mut EVMData<'_, DB>, + address: &Address, + topics: &[B256], + data: &Bytes, ) { + let _ = evm_data; + let _ = address; + let _ = topics; + let _ = data; } /// Called after `step` when the instruction has been executed. /// /// InstructionResulting anything other than [InstructionResult::Continue] alters the execution of the interpreter. + #[inline] fn step_end( &mut self, - _interp: &mut Interpreter, - _data: &mut EVMData<'_, DB>, - _eval: InstructionResult, + interp: &mut Interpreter, + data: &mut EVMData<'_, DB>, + eval: InstructionResult, ) -> InstructionResult { + let _ = interp; + let _ = data; + let _ = eval; InstructionResult::Continue } /// Called whenever a call to a contract is about to start. /// /// InstructionResulting anything other than [InstructionResult::Continue] overrides the result of the call. + #[inline] fn call( &mut self, - _data: &mut EVMData<'_, DB>, - _inputs: &mut CallInputs, + data: &mut EVMData<'_, DB>, + inputs: &mut CallInputs, ) -> (InstructionResult, Gas, Bytes) { + let _ = data; + let _ = inputs; (InstructionResult::Continue, Gas::new(0), Bytes::new()) } @@ -92,25 +102,31 @@ pub trait Inspector { /// /// InstructionResulting anything other than the values passed to this function (`(ret, remaining_gas, /// out)`) will alter the result of the call. + #[inline] fn call_end( &mut self, - _data: &mut EVMData<'_, DB>, - _inputs: &CallInputs, + data: &mut EVMData<'_, DB>, + inputs: &CallInputs, remaining_gas: Gas, ret: InstructionResult, out: Bytes, ) -> (InstructionResult, Gas, Bytes) { + let _ = data; + let _ = inputs; (ret, remaining_gas, out) } /// Called when a contract is about to be created. /// /// InstructionResulting anything other than [InstructionResult::Continue] overrides the result of the creation. + #[inline] fn create( &mut self, - _data: &mut EVMData<'_, DB>, - _inputs: &mut CreateInputs, + data: &mut EVMData<'_, DB>, + inputs: &mut CreateInputs, ) -> (InstructionResult, Option
, Gas, Bytes) { + let _ = data; + let _ = inputs; ( InstructionResult::Continue, None, @@ -123,18 +139,26 @@ pub trait Inspector { /// /// InstructionResulting anything other than the values passed to this function (`(ret, remaining_gas, /// address, out)`) will alter the result of the create. + #[inline] fn create_end( &mut self, - _data: &mut EVMData<'_, DB>, - _inputs: &CreateInputs, + data: &mut EVMData<'_, DB>, + inputs: &CreateInputs, ret: InstructionResult, address: Option
, remaining_gas: Gas, out: Bytes, ) -> (InstructionResult, Option
, Gas, Bytes) { + let _ = data; + let _ = inputs; (ret, address, remaining_gas, out) } /// Called when a contract has been self-destructed with funds transferred to target. - fn selfdestruct(&mut self, _contract: Address, _target: Address, _value: U256) {} + #[inline] + fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) { + let _ = contract; + let _ = target; + let _ = value; + } } diff --git a/crates/revm/src/inspector/customprinter.rs b/crates/revm/src/inspector/customprinter.rs index f0b8a4ae88..d431cea583 100644 --- a/crates/revm/src/inspector/customprinter.rs +++ b/crates/revm/src/inspector/customprinter.rs @@ -1,9 +1,10 @@ -//! Custom print inspector, it has step level information of execution. -//! It is a great tool if some debugging is needed. -//! use crate::interpreter::{opcode, CallInputs, CreateInputs, Gas, InstructionResult, Interpreter}; use crate::primitives::{Address, Bytes, U256}; use crate::{inspectors::GasInspector, Database, EVMData, Inspector}; + +/// Custom print [Inspector], it has step level information of execution. +/// +/// It is a great tool if some debugging is needed. #[derive(Clone, Default)] pub struct CustomPrintTracer { gas_inspector: GasInspector, diff --git a/crates/revm/src/inspector/tracer_eip3155.rs b/crates/revm/src/inspector/eip3155.rs similarity index 98% rename from crates/revm/src/inspector/tracer_eip3155.rs rename to crates/revm/src/inspector/eip3155.rs index 5625225f40..9346f73c28 100644 --- a/crates/revm/src/inspector/tracer_eip3155.rs +++ b/crates/revm/src/inspector/eip3155.rs @@ -1,5 +1,3 @@ -//! Inspector that support tracing of EIP-3155 - use crate::inspectors::GasInspector; use crate::interpreter::{CallInputs, CreateInputs, Gas, InstructionResult}; use crate::primitives::{db::Database, hex, Address, Bytes}; @@ -9,6 +7,7 @@ use revm_interpreter::{opcode, Interpreter, Memory, Stack}; use serde_json::json; use std::io::Write; +/// [EIP-3155](https://eips.ethereum.org/EIPS/eip-3155) tracer [Inspector]. pub struct TracerEip3155 { output: Box, gas_inspector: GasInspector, diff --git a/crates/revm/src/inspector/gas.rs b/crates/revm/src/inspector/gas.rs index 628137a9d6..22639a3edc 100644 --- a/crates/revm/src/inspector/gas.rs +++ b/crates/revm/src/inspector/gas.rs @@ -4,6 +4,7 @@ use crate::interpreter::{CallInputs, CreateInputs, Gas, InstructionResult}; use crate::primitives::{db::Database, Address, Bytes}; use crate::{evm_impl::EVMData, Inspector}; +/// Helper [Inspector] that keeps track of gas. #[allow(dead_code)] #[derive(Clone, Copy, Debug, Default)] pub struct GasInspector { @@ -32,18 +33,6 @@ impl Inspector for GasInspector { InstructionResult::Continue } - // get opcode by calling `interp.contract.opcode(interp.program_counter())`. - // all other information can be obtained from interp. - - #[cfg(not(feature = "no_gas_measuring"))] - fn step( - &mut self, - _interp: &mut crate::interpreter::Interpreter, - _data: &mut EVMData<'_, DB>, - ) -> InstructionResult { - InstructionResult::Continue - } - #[cfg(not(feature = "no_gas_measuring"))] fn step_end( &mut self, @@ -51,13 +40,8 @@ impl Inspector for GasInspector { _data: &mut EVMData<'_, DB>, _eval: InstructionResult, ) -> InstructionResult { - let last_gas = self.gas_remaining; - self.gas_remaining = interp.gas.remaining(); - if last_gas > self.gas_remaining { - self.last_gas_cost = last_gas - self.gas_remaining; - } else { - self.last_gas_cost = 0; - } + let last_gas = core::mem::replace(&mut self.gas_remaining, interp.gas.remaining()); + self.last_gas_cost = last_gas.saturating_sub(self.last_gas_cost); InstructionResult::Continue } diff --git a/crates/revm/src/inspector/noop.rs b/crates/revm/src/inspector/noop.rs index 12123b47f3..05d7292fc4 100644 --- a/crates/revm/src/inspector/noop.rs +++ b/crates/revm/src/inspector/noop.rs @@ -1,8 +1,7 @@ -//! Dummy NoOp Inspector, helpful as standalone replacement. - use crate::{Database, Inspector}; -#[derive(Clone, Copy)] +/// Dummy [Inspector], helpful as standalone replacement. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct NoOpInspector; impl Inspector for NoOpInspector {} diff --git a/crates/revm/src/journaled_state.rs b/crates/revm/src/journaled_state.rs index 03901e4ba0..2e5de6f548 100644 --- a/crates/revm/src/journaled_state.rs +++ b/crates/revm/src/journaled_state.rs @@ -1,4 +1,4 @@ -use crate::interpreter::{inner_models::SelfDestructResult, InstructionResult}; +use crate::interpreter::{InstructionResult, SelfDestructResult}; use crate::primitives::{ db::Database, hash_map::Entry, Account, Address, Bytecode, HashMap, Log, Spec, SpecId::*, State, StorageSlot, TransientStorage, KECCAK_EMPTY, PRECOMPILE3, U256, diff --git a/crates/revm/src/lib.rs b/crates/revm/src/lib.rs index fbaeab6c18..61c0e5ae14 100644 --- a/crates/revm/src/lib.rs +++ b/crates/revm/src/lib.rs @@ -28,7 +28,7 @@ pub use db::{ pub use db::{Database, DatabaseCommit, InMemoryDB}; pub use evm::{evm_inner, new, EVM}; -pub use evm_impl::{EVMData, EVMImpl, Transact}; +pub use evm_impl::{EVMData, EVMImpl, Transact, CALL_STACK_LIMIT}; pub use journaled_state::{is_precompile, JournalCheckpoint, JournalEntry, JournaledState}; // reexport `revm_precompiles`